spring security 返回403状态码
spring boot项目集成spring security后输入正确的用户名和密码后仍然返回403 Forbidden错误。
观察发现get请求没有问题,post请求返回上面的http异常码。
原因是spring security中未配置csrf().disable()。默认开启csrf防护,post请求需要传递csrf token。我们curl或者postman模拟请求时没有带这个参数。一般直接禁掉csrf token检测即可。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
//.csrf().disable()
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/proxy/ip/**").permitAll()
.anyRequest().authenticated().and()
.httpBasic();
}
}