【问题标题】:How to disable csrf in Spring using application.properties?如何使用 application.properties 在 Spring 中禁用 csrf?
【发布时间】:2017-12-03 02:39:16
【问题描述】:

存在以下属性:

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

    }
}

【问题讨论】:

  • 只有在允许 Spring Boot 配置安全性的情况下才有效,如果您自己在搞乱安全性,该属性将不会做任何事情。还要确保您使用的是支持该属性的 Spring Boot 版本。
  • 我在最新的-1.5.4 上,我只添加了configure() 方法来禁用csrf。如果我完全删除该方法,该属性仍然不会被考虑在内。唯一的自定义安全配置是我用来设置BCryptPasswordEncoderconfigure(AuthenticationManagerBuilder auth)。但这不应该影响 csrf。
  • 这取决于,如果你有 @EnableWebMvc 它会禁用自动配置。
  • @SpringBootApplication 是我唯一的注释。我正在扩展WebSecurityConfigurerAdapter。也许这就是原因?
  • 你在使用 Spring Boot 吗?因为你的问题并不清楚......

标签: java spring spring-mvc spring-boot spring-security


【解决方案1】:

由于 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();
       }
    }
}

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

--- application.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 日更新

基于comment of a Spring Boot member,此问题已在新版本的 Spring 上得到修复:我在版本 1.5.2.RELEASE 上有它,但似乎在版本 1.5.9.RELEASE(版本 2 之前的最新稳定版本)中它已经已修复,默认情况下 csrf 已禁用,可以使用 security.enable_csrf: true 启用。因此,一种可能的解决方案可能只是升级到版本1.5.9.RELEASE,然后再将主要版本升级到架构可能更加不同的版本2。

【讨论】:

  • 谢谢,这是个好主意。虽然它仍然无法解释为什么仅通过设置配置属性就不能完全禁用 crsf 配置。
  • 应该在他们的 Spring github 和/或 jira 中提出问题
  • Spring Security documenation for CSRF 中它说默认情况下它是打开的,要禁用它,您必须通过 Java 或 xml 代码执行此操作。 .properties 属性应该由 Spring Boot 实现:在Spring Boot 中打开的问题
  • CSRF 从 Spring Security 4.0 开始默认启用。 docs.spring.io/spring-security/site/docs/4.2.1.RELEASE/…
  • 假设我通过外部配置将其“禁用”并且我正在运行我的应用程序。现在在某个时间点我想“启用” CSRF,所以我将属性更改为 TRUE 并点击 /refresh 端点,但这是否意味着 Spring 容器将再次调用“AuthConfig”的“configure”方法?如果没有,那么我需要重新暂存我的应用程序或重新启动我的应用程序,以便 spring 容器加载新配置。
【解决方案2】:

更新:

在 spring-boot 1.x 上使用 application.properties 禁用 CSRF 似乎存在问题(感谢 Eliux 打开此 case)。

所以我的 spring-boot 1.5.7 解决方案与嵌入式 tomcat 是通过 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");
    }
} 

【讨论】:

  • 似乎对于 1.5.9 或更高版本这样的版本是固定的。
猜你喜欢
  • 2019-11-07
  • 2018-07-21
  • 2017-03-12
  • 2020-08-18
  • 2016-06-19
  • 2020-11-21
  • 2020-07-17
  • 2021-05-06
  • 1970-01-01
相关资源
最近更新 更多