Spring Security 返回 200 而不是 HttpWebHandlerAdapter 所述的 401

Posted

技术标签:

【中文标题】Spring Security 返回 200 而不是 HttpWebHandlerAdapter 所述的 401【英文标题】:Spring Security returning 200 instead of 401 as stated by HttpWebHandlerAdapter 【发布时间】:2021-07-28 06:31:43 【问题描述】:

试图弄清楚我是否刚刚在 Spring Security 中发现了一个错误,这是针对最新的 2.4.5 版本的。 HttpWebHandlerAdapter 声明它在日志中返回 401,但 Postman 中的响应是 200。下面是相关的 spring 安全配置/处理程序等。

Spring 安全配置

@Configuration
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity(proxyTargetClass = true)
@RequiredArgsConstructor
public class SecurityConfig 
  private final JwtAuthenticationConverter jwtAuthenticationConverter;
  private final UsersRepository usersRepository;
  private final UserRolesRepository userRolesRepository;
  private final RoleScopesRepository roleScopesRepository;
  private final JwtUtil jwtUtil;

  private static Map<HttpMethod, String[]> AUTH_WHITELIST =
      Map.of(
          // Public auth endpoints
          HttpMethod.PUT, new String[] "/v1/auth/login",
          HttpMethod.POST, new String[] "/v1/auth/register",
          HttpMethod.GET,
              new String[] 
                // Actuator
                "/actuator",
                "/actuator/health*",
                "/actuator/info",
                // SpringFox/OpenAPI
                "/v3/api-docs/**",
                "/swagger-ui/**",
                "/swagger-resources/**",
                "/webjars/swagger-ui/**",
                // Public API endpoints
                "/v1/posts/*/comments/*",
                "/v1/posts/*/comments",
                "/v1/posts/*",
                "/v1/posts"
              );

  @Bean
  public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) 
    // Build path/verb matchers
    Set<ServerWebExchangeMatcher> matchers = new HashSet<>();
    AUTH_WHITELIST.forEach(
        (method, paths) -> matchers.add(ServerWebExchangeMatchers.pathMatchers(method, paths)));
    ServerWebExchangeMatcher[] matchersArray = matchers.toArray(new ServerWebExchangeMatcher[0]);

    return http.addFilterAt(
            getAuthenticationWebFilter(matchersArray), SecurityWebFiltersOrder.AUTHENTICATION)
        .authorizeExchange()
        .matchers(matchersArray)
        .permitAll()
        .anyExchange()
        .authenticated()
        .and()
        .formLogin()
        .and()
        .csrf()
        .disable()
        .cors()
        .configurationSource(createCorsConfigSource())
        .and()
        .formLogin()
        .disable()
        .httpBasic()
        .disable()
        .logout()
        .disable()
        .build();
  

  public CorsConfigurationSource createCorsConfigSource() 
    org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource source =
        new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.addAllowedOrigin("http://localhost:3000");
    config.setAllowedMethods(List.of("OPTIONS", "GET", "PUT", "POST", "DELETE"));
    source.registerCorsConfiguration("/**", config);
    return source;
  

  private AuthenticationWebFilter getAuthenticationWebFilter(
      ServerWebExchangeMatcher[] matchersArray) 
    // Create web filter with custom user details service/authentication manager
    AuthenticationWebFilter authenticationWebFilter =
        new AuthenticationWebFilter(new AuthenticationManager(customUserDetailsService()));
    // Set custom JWT authentication converter
    authenticationWebFilter.setServerAuthenticationConverter(jwtAuthenticationConverter);
    // Negate whitelist to set paths with required authentication
    NegatedServerWebExchangeMatcher negateWhiteList =
        new NegatedServerWebExchangeMatcher(ServerWebExchangeMatchers.matchers(matchersArray));
    authenticationWebFilter.setRequiresAuthenticationMatcher(negateWhiteList);
    // Add failure handler
    authenticationWebFilter.setAuthenticationFailureHandler(new AuthenticationFailureHandler());
    return authenticationWebFilter;
  

  @Bean
  public PasswordEncoder passwordEncoder() 
    return new BCryptPasswordEncoder();
  

  @Bean
  @Primary
  public UserDetailsService customUserDetailsService() 
    return new UserDetailsService(
        new UserService(
            usersRepository,
            userRolesRepository,
            roleScopesRepository,
            passwordEncoder(),
            jwtUtil));
  

故障处理程序

@Slf4j
public class AuthenticationFailureHandler implements ServerAuthenticationFailureHandler 
  @Override
  public Mono<Void> onAuthenticationFailure(
      WebFilterExchange webFilterExchange, AuthenticationException exception) 
    log.warn(exception.getMessage());
    ServerHttpResponse response = webFilterExchange.getExchange().getResponse();
    response.setStatusCode(HttpStatus.UNAUTHORIZED);
    response.getHeaders().addIfAbsent(HttpHeaders.LOCATION, "/");
    response.setComplete();
    return Mono.error(exception);
  

JWT 身份验证转换器。

@Slf4j
@RequiredArgsConstructor
@Component
public class JwtAuthenticationConverter implements ServerAuthenticationConverter 
  private final JwtUtil jwtUtil;

  private Mono<String> extractJwtFromAuthorizationHeader(ServerWebExchange exchange) 
    return Mono.justOrEmpty(exchange.getRequest().getHeaders().get(HttpHeaders.AUTHORIZATION))
        // Remove empty headers/headers with empty string as value
        .filter(
            header ->
                !header.isEmpty()
                    && StringUtils.hasText(header.get(0))
                    && header.get(0).contains("Bearer"))
        .map(header -> header.get(0).replaceAll(AuthConstants.BEARER_PREFIX_REGEX, ""))
        .switchIfEmpty(Mono.error(new InvalidJwtException("Invalid bearer token")));
  

  @Override
  public Mono<Authentication> convert(ServerWebExchange exchange) 
    return Mono.justOrEmpty(exchange)
        .flatMap(this::extractJwtFromAuthorizationHeader)
        .map(jwtUtil::getAuthenticationFromJwt);
  

身份验证管理器

public class AuthenticationManager extends UserDetailsRepositoryReactiveAuthenticationManager 

  public AuthenticationManager(ReactiveUserDetailsService userDetailsService) 
    super(userDetailsService);
  

  @Override
  public Mono<Authentication> authenticate(Authentication authentication) 
    return authentication.isAuthenticated()
        ? Mono.just(authentication)
        : super.authenticate(authentication);
  

相关日志

2021-05-05 15:41:18.981 DEBUG 1984531 --- [or-http-epoll-3] o.s.w.s.h.ResponseStatusExceptionHandler : [82168f6e-13] Resolved [InvalidJwtException: Unsupported JWT token: The parsed JWT indicates it was signed with the HS512 signature algorithm, but the specified signing key of type sun.security.rsa.RSAPublicKeyImpl may not be used to validate HS512 signatures.  Because the specified signing key reflects a specific and expected algorithm, and the JWT does not reflect this algorithm, it is likely that the JWT was not expected and therefore should not be trusted.  Another possibility is that the parser was configured with the incorrect signing key, but this cannot be assumed for security reasons.] for HTTP POST /v1/posts
2021-05-05 15:41:18.981 DEBUG 1984531 --- [or-http-epoll-3] o.s.w.s.adapter.HttpWebHandlerAdapter    : [82168f6e-13] Completed 401 UNAUTHORIZED

【问题讨论】:

不是错误,失败处理程序上的 setComplete() 是有问题的行。日志显示 401 时不再有 200 秒。 【参考方案1】:

不是错误,失败处理程序上的 setComplete() 是有问题的行。日志显示 401 时不再是 200。

【讨论】:

以上是关于Spring Security 返回 200 而不是 HttpWebHandlerAdapter 所述的 401的主要内容,如果未能解决你的问题,请参考以下文章

Spring Security 禁用登录页面/重定向

Spring Security - 会话超时而不扩展/更新

Spring Security:会话过期而不重定向到过期网址?

Spring Security jwt 身份验证

如何在 Spring Security 中阻止 LDAP 用户帐户而不锁定 LDAP 用户?

REST端点身份验证的Spring Security意外行为?