无法使用Spring Boot REST API设置基本身份验证
我正在尝试使用Spring Boot设置RESTful API,并尝试启用基本身份验证。为什么我总是遇到403拒绝访问错误?我在邮递员中以邮件头的形式发送我的凭证(见附图)。如果我删除无法使用Spring Boot REST API设置基本身份验证,spring,spring-boot,spring-security,Spring,Spring Boot,Spring Security,我正在尝试使用Spring Boot设置RESTful API,并尝试启用基本身份验证。为什么我总是遇到403拒绝访问错误?我在邮递员中以邮件头的形式发送我的凭证(见附图)。如果我删除.anyRequest.authenticated(),它可以正常工作。但我不想删除它,因为我希望每个端点都有基本的身份验证。我做错了什么 Application.java SecurityConfiguration.java Controller.java 在翻阅了Spring文档之后,我似乎理解了每个链式方法调
.anyRequest.authenticated()
,它可以正常工作。但我不想删除它,因为我希望每个端点都有基本的身份验证。我做错了什么
Application.java
SecurityConfiguration.java
Controller.java
在翻阅了Spring文档之后,我似乎理解了每个链式方法调用的用途 无论如何,简单的答案是我需要
和().httpBasic()
通过RESTAPI启用基本HTTP身份验证
.anyRequest().authenticated()
只是要求对每个请求进行身份验证,但没有指定方法。添加基本身份验证意味着我们可以使用基本身份验证对用户进行身份验证
在翻阅了Spring文档之后,我似乎理解了每个链式方法调用的用途 无论如何,简单的答案是我需要
和().httpBasic()
通过RESTAPI启用基本HTTP身份验证
.anyRequest().authenticated()
只是要求对每个请求进行身份验证,但没有指定方法。添加基本身份验证意味着我们可以使用基本身份验证对用户进行身份验证
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/h2-console/*").permitAll()
.anyRequest().authenticated();
http.csrf().disable();
http.headers().frameOptions().disable();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
@RestController
public class Controller {
@RequestMapping("/test")
public String index() {
return "Greetings from Spring Boot!";
}
}