X-CSRF-TOKEN 不是由 Spring Boot 生成的

Bru*_*ola 8 java spring csrf spring-security spring-boot

我按照这里的指南:http : //spring.io/guides/gs/rest-service/来构建我的休息服务示例,现在我正在尝试启用 CSRF 保护。我读到它应该默认启用,所以如果我不包括:

http.csrf().disable()

在我的WebSecurityConfigurerAdapter配置中,CSRF 保护应该默认启用,但似乎并非如此。问题是 X-CSRF-TOKEN 没有生成,也没有以任何方式包含在我的 HTTP 响应中。我应该怎么做,让 x-csrf-token 生成并包含在响应中,当然还有 csrf 保护完全起作用?

我注意到,使用类似的 spring mvc 配置,我生成的 x-csrf-token 只包括:

< security:csrf disabled="false"/>

在我的安全配置文件中。但是,使用 Spring Boot 时,我可能会出错,并且无法生成 csrf 令牌。任何人都可以帮助我,也许给我指出一个有效的例子?我的安全配置是:

     @Override
     protected void configure(HttpSecurity http) throws Exception 
     {
        http
      // .csrf().disable()
      .authorizeRequests()
          .anyRequest()
          .authenticated()
      .and()
      .httpBasic()
      .authenticationEntryPoint(new RestAuthenticationEntryPoint())
      .and()
      .formLogin()
      .successHandler(new RestAuthenticationSuccessHandler())
      .failureHandler(new SimpleUrlAuthenticationFailureHandler())
      .and()
      .logout()
      .logoutSuccessHandler(new RestLogoutSuccessHandler());
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception 
{
    auth.userDetailsService(restUserDetailService);
}
Run Code Online (Sandbox Code Playgroud)

jea*_*ara 5

要在您的 csrf 保护中包含 CSRF 令牌,您可以包含 CSRFTokenRepository 以生成令牌。为了说明您的情况,添加一条简单的行就足够了:

 @Override
 protected void configure(HttpSecurity http) throws Exception 
 {
  http.
  .csrf()
  .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) //HERE !  Defaults XSRF-TOKEN as cookie name and X-XSRF-TOKEN as header name  
  .authorizeRequests()
  .anyRequest()
  .authenticated()
  .and()
  .httpBasic()
  .authenticationEntryPoint(new RestAuthenticationEntryPoint())
  .and()
  .formLogin()
  .successHandler(new RestAuthenticationSuccessHandler())
  .failureHandler(new SimpleUrlAuthenticationFailureHandler())
  .and()
  .logout()
  .logoutSuccessHandler(new RestLogoutSuccessHandler());}
Run Code Online (Sandbox Code Playgroud)


Tür*_*rci 0

我们在安全测试期间遇到了非常类似的问题,我们怀疑我们不小心在 websecurityconfig 类的配置方法中禁用了 csfr,默认情况下它是启用的。通过如下所示更改 configfigure 方法,我们让 spring 自动生成 csfr 令牌。

websecurityconfig类配置方法==>

@Override
protected void configure(HttpSecurity http) throws Exception {              

     http
     .authorizeRequests()
         .antMatchers("/", "/login","/loginError","/home","/interruption").permitAll()                                                          
         .antMatchers("/admin").hasAuthority(Roles.ROLE_PREFIX.role()+Roles.HALLEYYNT01.role())  
         .antMatchers("/requests").hasAuthority(Roles.ROLE_PREFIX.role()+Roles.CCHALLEYLOGIN.role())
         .antMatchers("/solrequests").hasAuthority(Roles.ROLE_PREFIX.role()+Roles.SOLHALLEYLOGIN.role())
         .anyRequest().authenticated()
         .and()
     .formLogin()             
         .loginPage("/login")  
         //.failureUrl("/loginError")
         .loginProcessingUrl("/authenticate")
         .defaultSuccessUrl("/")            
         .and()
     .logout().clearAuthentication(true).invalidateHttpSession(true).deleteCookies("JSESSIONID")         
         .logoutSuccessUrl("/login");
         //.and() 
     //.exceptionHandling().accessDeniedHandler(accessDeniedHandler);    
}   
Run Code Online (Sandbox Code Playgroud)