spring security POST请求 报403 Forbidden

spring security POST请求 报403 Forbidden

Security配置代码:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 配置不需要验证
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().permitAll().and().logout().permitAll();//配置不需要登录验证
    }
    

}

配置那么就不需要认证就可以请求接口
在这里插入图片描述
但是一样的接口使用post就会有问题
在这里插入图片描述
为了查看这个请示具体的执行过程,把日志级别调整到debug,看一下是否有收获

查阅资料后发现这是一个RESTful技术与CSRF(Cross-site request forgery跨站请求伪造)的冲突造成的,CSRF默认支持的方法: GET|HEAD|TRACE|OPTIONS,不支持POST。可以在security在配置中禁用掉它。

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 配置不需要验证
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().permitAll().and().logout().permitAll().and()
                .csrf().disable();//配置不需要登录验证
    }


}

在这里插入图片描述