当前位置: 代码迷 >> 综合 >> SpringBoot + SpringSecurity解决POST DELETE方式下的被拒绝访问 报错403的问题 (关闭CSRF)
  详细解决方案

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

热度:21   发布时间:2024-01-20 21:30:53.0

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 {@Overrideprotected void configure(HttpSecurity http) throws Exception {//super.configure(http); http.csrf().disable();  //关闭CSRF}}

 

  相关解决方案