存在以下属性:
security.enable-csrf=false
但是如果我将属性添加到 application.properties
,csrf 保护仍然有效。
有效的是以编程方式禁用它。
但我更喜欢属性配置。为什么它不能工作?
@Configuration
public class AuthConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.csrf().disable();
}
}
原文由 membersound 发布,翻译遵循 CC BY-SA 4.0 许可协议
由于
WebSecurityConfigurerAdapter
使用命令式方法,您可以注入security.enable-csrf
变量的值并在它为 false 时禁用 CSRF。你是对的,我认为这应该开箱即用。我所做的是在我的 application.yml 中将该变量设置为 false,因为当我有一个 dev spring 配置文件处于活动状态时,尽管您也可以为此目的创建一个名为 nosecurity 的配置文件。它大大简化了这个过程:
--- 应用程序.yml —
我希望它适合你的需要
2017 年 12 月 17 日更新
根据 Spring Boot 成员的评论, 此问题已在新版本的 Spring 上修复:我在版本
1.5.2.RELEASE
上有它,但似乎在版本 1.5.9.RELEASE (迄今为止最新的稳定版)中在版本 2 之前)它已经修复并且默认情况下 csrf 被禁用并且可以使用security.enable_csrf: true
启用它。因此,一个可能的解决方案可能只是升级到版本1.5.9.RELEASE
,然后再将主要版本升级到版本 2,其中架构可能会有很大不同。