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
}
}

789

被折叠的 条评论
为什么被折叠?



