【问题标题】:Spring security csrf disabled, still get an Invalid CSRF token foundSpring security csrf disabled,仍然得到一个 Invalid CSRF token found
【发布时间】:2020-03-25 01:34:57
【问题描述】:

我在使用 csrf 时遇到问题,即使它在 spring 配置中被禁用。 我的日志输出如下:

Invalid CSRF token found for http://localhost:8080/exercise/

我有这个弹簧配置

    protected void configure(HttpSecurity http)throws Exception {
    http.csrf().disable();
    http
            .httpBasic()
            .and()
            .authorizeRequests()
            .antMatchers(HttpMethod.POST, "/user/add").hasRole("ADMIN")
            .antMatchers(HttpMethod.POST).hasAnyRole("ADMIN", "TRAINER")
            .antMatchers(HttpMethod.DELETE).hasAnyRole("ADMIN", "TRAINER")
            .antMatchers(HttpMethod.GET).permitAll()
            .antMatchers(HttpMethod.PUT).permitAll()
            .antMatchers(HttpMethod.HEAD).permitAll()
            .antMatchers("/register/").authenticated()
            .and()
            .exceptionHandling()
            .and()
            .formLogin()
            .permitAll()
            .and()
            .logout()
            .permitAll();
   }

好像我获得了身份验证和授权,因为日志输出:

Obtained a valid SecurityContext from SPRING_SECURITY_CONTEXT: 'org.springframework.security.core.context.SecurityContextImpl@1ddb0b4b: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@1ddb0b4b: Principal: com.brevisfit.api.model.user.User[ iduser=1 ]; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ADMIN, ROLE_TRAINER'

我对 POST\DELETE\PUT 请求有疑问,因为它们会抛出 403 响应,我知道它来自 CsrfFilter.class

if (!csrfToken.getToken().equals(actualToken)) {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Invalid CSRF token found for "
                        + UrlUtils.buildFullRequestUrl(request));
            }

对于请求,我使用的是邮递员。

【问题讨论】:

  • 你把这个configure方法放在哪里了?是否在扩展 WebSecurityConfigurerAdapter 的配置类中?
  • 您找到了吗,出了什么问题?或者你是怎么解决的?

标签: java spring spring-boot csrf


【解决方案1】:

您的 AuthenticationBuilder 应该是 @Autowired。

@Autowired
    protected void configure(AuthenticationManagerBuilder auth) {
        auth.authenticationProvider(authenticationProvider());

    }

See this

【讨论】:

  • 在我的 AuthenticationManagerBuilder 配置方法中添加了 @autowired,但没有成功。另外,我的问题与您引用的问题不同。至于我,我得到了所有正确的授权。它是抛出异常的csrf过滤器
猜你喜欢
  • 2017-01-07
  • 2021-04-28
  • 2015-12-10
  • 2014-10-21
  • 2015-02-11
  • 2016-01-12
  • 2018-03-05
  • 2015-03-05
  • 2013-07-27
相关资源
最近更新 更多