找不到预期的 CSRF 令牌

Pet*_*zov 5 spring spring-security spring-boot spring-oauth2

我正在尝试使用以下配置禁用最新 Spring Cloud 中的 Spring 安全性:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
@Order(SecurityProperties.DEFAULT_FILTER_ORDER)
public class WebSecurityConfigSec extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().cors().disable()
                .authorizeRequests().anyRequest().permitAll();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers("/**");
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序.yml

spring:
    main:
        allow-bean-definition-overriding: true
security:
    ignored=/**:
    enable-csrf: false
Run Code Online (Sandbox Code Playgroud)

我还尝试添加:

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends
   WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .csrf().disable();
  }
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

我收到错误:An expected CSRF token cannot be found

18:16:24.537 [boundedElastic-2] DEBUG DefaultWebSessionManager[lambda$createWebSession$3:94] - Created new WebSession.
18:16:24.540 [boundedElastic-2] DEBUG HttpWebHandlerAdapter[traceDebug:91] - [1ffd0a30] Completed 403 FORBIDDEN
Run Code Online (Sandbox Code Playgroud)

你知道我该如何解决这个问题吗?

小智 -1

从 pom.xml 中排除 MVC 依赖项

并添加:

spring:
 main:
  web-application-type: reactive
Run Code Online (Sandbox Code Playgroud)

这对我有用;由于启用了 Spring MVC 中使用的 Spring Security,我收到了 CSRF 错误。