如何使用 application.properties 在 Spring 中禁用 csrf?

新手上路,请多包涵

存在以下属性:

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 许可协议

阅读 1.2k
2 个回答

由于 WebSecurityConfigurerAdapter 使用命令式方法,您可以注入 security.enable-csrf 变量的值并在它为 false 时禁用 CSRF。你是对的,我认为这应该开箱即用。

 @Configuration
public class AuthConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;

    @Value("${security.enable-csrf}")
    private boolean csrfEnabled;

    @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);

       if(!csrfEnabled)
       {
         http.csrf().disable();
       }
    }
}

我所做的是在我的 application.yml 中将该变量设置为 false,因为当我有一个 dev spring 配置文件处于活动状态时,尽管您也可以为此目的创建一个名为 nosecurity 的配置文件。它大大简化了这个过程:

--- 应用程序.yml —

 # Production configuration
server:
  port: ${server.web.port}
admin.email: ${admin.email}
#etc
---
spring:
  profiles: dev

security.enable-csrf: false

#other Development configurations

我希望它适合你的需要

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,其中架构可能会有很大不同。

原文由 EliuX 发布,翻译遵循 CC BY-SA 3.0 许可协议

更新:

在 spring-boot 1.x 上使用 application.properties 禁用 CSRF 似乎存在问题(感谢 Eliux 打开了这个 案例)。

因此,我针对带有嵌入式 tomcat 的 spring-boot 1.5.7 的解决方案是通过 SecurityConfig 类禁用 CSRF(请注意,通过这种方式我保留了 tomcat ootb 基本身份验证):

 @EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // Note:
        // Use this to enable the tomcat basic authentication (tomcat popup rather than spring login page)
        // Note that the CSRf token is disabled for all requests (change it as you wish...)
        http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        // Add here any custom code you need in order to get the credentials from the user...
        auth.inMemoryAuthentication()
            .withUser("myUserName")
            .password("myPassword")
            .roles("USER");
    }
}

原文由 Naor Bar 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题