【问题标题】:Spring Boot 3.0 + Security 6 +WebFlux causes "An expected CSRF token cannot be found" in PostmanSpring Boot 3.0 + Security 6 +WebFlux 导致 Postman 中“找不到预期的 CSRF 令牌”
【发布时间】:2022-11-26 16:08:45
【问题描述】:

以下安全Web过滤器链在 Spring Boot 2.7.x 中工作得很好,但在 Spring Boot 3.0.0 中不再工作。它只是显示“找不到预期的 CSRF 令牌” 在 Postman 中调用 REST API 时。请教我如何解决它好吗?

@Bean
public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
    
    
    http
            .cors().disable()               
            .csrf().disable()
            
            .exceptionHandling()
            .authenticationEntryPoint((swe, e) -> 
                Mono.fromRunnable(() -> swe.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED))
            ).accessDeniedHandler((swe, e) -> 
                Mono.fromRunnable(() -> swe.getResponse().setStatusCode(HttpStatus.FORBIDDEN))
            )
            .and()
            .authenticationManager(authenticationManager)
            .securityContextRepository(securityContextRepository)
            .authorizeExchange(exchange -> exchange                                     
                    .pathMatchers(HttpMethod.OPTIONS).permitAll()
                    .pathMatchers("/login", "/register").permitAll()                        
                    .anyExchange().authenticated()
                    .and()
                    .cors().disable()
                    .csrf().disable()
            )
            .formLogin().disable()
            .httpBasic().disable()   
            ;
            
    return http.csrf(csrf -> csrf.disable()).build();
}

【问题讨论】:

  • 您确定正在接收这个SecurityWebFilterChain 而不是另一个?

标签: spring-boot spring-security csrf spring-boot-3


【解决方案1】:

你可以试试看

https://docs.spring.io/spring-security/reference/servlet/oauth2/resource-server/jwt.html

应用.yml

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: https://idp.example.com/issuer

【讨论】:

    猜你喜欢
    • 2017-05-02
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2017-12-20
    • 2017-09-07
    • 1970-01-01
    • 2019-12-30
    • 2015-06-13
    相关资源
    最近更新 更多