我使用 Spring Security 进行基本身份验证以保护我的 REST API。
下面是配置代码:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user")
.password("password")
.roles("admin");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.anyRequest().authenticated();
}
}
我在使用正确的用户名和密码进行身份验证时遇到禁止 (403) 错误。
请建议修改以使其正常工作。
最佳答案
您还没有启用 HTTP 基本身份验证,您必须使用 HttpSecurity.httpBasic()
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception{
auth.inMemoryAuthentication().withUser("user").password("password").roles("admin");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.httpBasic()
.and()
.authorizeRequests()
.anyRequest().authenticated();
}
}
关于java - 基本身份验证后 Spring Security 抛出 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41567640/