Reactive-Spring-Security-5.1.3.RELEASE,多重授权

bre*_*Dev 2 spring-security spring-boot spring-webflux

我们有一些端点是安全的,在访问它们之前,我们正在验证 jws 是否正确。为了做到这一点,我们定义了一个 SecurityContext 来实际持久化 Auth pojo 并在下游将其操作到控制器中。SecurityWebFilterChain 配置如下所示:

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
    return http.csrf().disable()
            .formLogin().disable()
            .logout().disable()
            .httpBasic().disable()
            .securityContextRepository(securityContext)
            .authorizeExchange()
            .anyExchange().authenticated()
            .and()
            .build();
}
Run Code Online (Sandbox Code Playgroud)

调用是内部进行的,我们只是验证了 jws 令牌。

现在一些外部客户端需要与我们集成,我们需要验证一个 jwe 令牌。问题是,我们需要以某种方式告诉 spring-security 验证现有端点 jws 和新端点 jwe。

我尝试指定多个安全匹配器,但失败了:(。你还有其他建议吗?

jzh*_*aux 13

您可以公开多个 bean。我建议指定一个顺序:

@Bean
@Order(1)
public SecurityWebFilterChain first(ServerHttpSecurity http) {
    http
        .securityMatcher(...)
        ...

    return http.build();
}

@Bean
@Order(2)
public SecurityWebFilterChain second(ServerHttpSecurity http) {
   http
       .securityMatcher(...)
       ...

   return http.build();
}
Run Code Online (Sandbox Code Playgroud)

附带说明一下,Spring Security 确实提供了对响应式验证 JWS 令牌的支持,并且您可以通过使用它来删除一些样板。

  • 我可以确认提供两个不同的 SecurityWebFilterChain beans 是有效的。似乎需要指定 securityMatcher。 (2认同)