SpringBoot + SpringSecurity解决POST DELETE方式下的被拒绝访问 报错403的问题 (关闭CSRF)

SpringBoot中配置了SSL之后,发现除了小程序访问后台的GET方法, 其余POST,DELETE都被拒绝,并且报错403.

找了好久原来是因为在Security的默认拦截器里,默认会开启CSRF处理,判断请求是否携带了token。

如果没有就拒绝访问。并且,在请求为(GET|HEAD|TRACE|OPTIONS)时,则不会开启。

解决

既然是因为默认开启了CSRF,那关掉即可。可以加入以下代码关闭~

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfigSEVEN extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http); 
        http.csrf().disable();  //关闭CSRF
    }

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值