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

问题描述 投票:10回答:2

存在以下属性:

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

    }
}
java spring spring-mvc spring-boot spring-security
2个回答
7
投票

由于WebSecurityConfigurerAdapter使用命令式方法,您可以注入security.enable-csrf变量的值,并在它为假时禁用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的配置文件。它简化了这个过程:

--- 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,其中架构可能会更加不同。


3
投票

更新:

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

所以我的带有嵌入式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");
    }
} 
© www.soinside.com 2019 - 2024. All rights reserved.