Spring Boot Security WebFlux 功能正常,无法为两种身份验证方法创建配置

5 spring spring-security spring-boot spring-webflux

我想在 Spring Boot 应用程序中有两种身份验证方式。应用程序是使用 WebFlux 和函数式方法编写的。对于我的某些端点,我希望使用 basicHttp 进行身份验证,对于某些端点,我希望使用自定义 HTTP 标头进行 JWT 身份验证。但是我无法合并两个 WebFilterChains 或使其作为一个整体工作。当时只有其中一种方法有效,不能同时使两种方法都有效。JWT 的代码尚未完成,但当我使用 httpBasic auth 方法发送请求时,我希望看到 401。示例中的路径是假的。

我的尝试:

1)

第一个豆:

          http
            .securityMatcher(ServerWebExchangeMatchers.pathMatchers(HttpMethod.GET, "/api/1/**"))
            .csrf().disable()
            .formLogin().disable()
            .logout().disable()
            .httpBasic().and()
            .authenticationManager(authenticationManager(userService))
            .build();
Run Code Online (Sandbox Code Playgroud)

第二个豆:

          http
            .securityMatcher(ServerWebExchangeMatchers.pathMatchers("/api/2/**"))
            .httpBasic().disable()
            .csrf().disable()
            .formLogin().disable()
            .logout().disable()
            .authenticationManager(tokenAuthManager)
            .authorizeExchange()
            .anyExchange().authenticated()
            .and()
            .build();
Run Code Online (Sandbox Code Playgroud)

2)

           http
            .securityMatcher(ServerWebExchangeMatchers.pathMatchers(HttpMethod.GET, "/api/1/**"))
            .csrf().disable()
            .formLogin().disable()
            .logout().disable()
            .httpBasic().and()
            .authenticationManager(authenticationManager(userService))
            .securityMatcher(ServerWebExchangeMatchers.pathMatchers("/api/2/**"))
            .httpBasic().disable()
            .csrf().disable()
            .formLogin().disable()
            .logout().disable()
            .authenticationManager(tokenAuthManager)
            .authorizeExchange()
            .anyExchange().authenticated()
            .and()
            .build();
Run Code Online (Sandbox Code Playgroud)