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

mem*_*und 13 java spring spring-mvc spring-security spring-boot

存在以下属性:

security.enable-csrf=false

但是如果我将属性添加到csrf保护仍然打开application.properties.

有效的是以编程方式禁用它.

但我更喜欢属性配置.为什么它不能工作?

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

    }
}
Run Code Online (Sandbox Code Playgroud)

Eli*_*iuX 8

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();
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

我所做的是在我的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
Run Code Online (Sandbox Code Playgroud)

我希望它符合您的需求

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

  • 从 Spring Security 4.0 开始,CSRF 默认启用。https://docs.spring.io/spring-security/site/docs/4.2.1.RELEASE/reference/htmlsingle/#csrf-configure (2认同)

Emm*_*ven 7

对于 Spring Boot 3.1.0 (spring-boot-starter-web),请使用下面的代码片段

http.csrf(AbstractHttpConfigurer::disable);
Run Code Online (Sandbox Code Playgroud)

对于 Spring Boot 3.1.0 (spring-boot-starter-webflux) 使用下面的代码片段

http.csrf(ServerHttpSecurity.CsrfSpec::disable);
Run Code Online (Sandbox Code Playgroud)


Nao*_*Bar 5

更新:

看起来在 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");
    }
} 
Run Code Online (Sandbox Code Playgroud)


小智 5

对于 Spring Boot 3。它帮助了我:

http.csrf(csrf -> csrf.disable());
Run Code Online (Sandbox Code Playgroud)