【问题标题】:Reactive-Spring-Security-5.1.3.RELEASE, multiple authorizationsReactive-Spring-Security-5.1.3.RELEASE,多重授权
【发布时间】:2019-07-13 22:54:07
【问题描述】:

我们有一些端点,它们是安全的,在访问它们之前,我们正在验证 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();
}

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

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

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

【问题讨论】:

    标签: spring-boot spring-security spring-webflux


    【解决方案1】:

    您可以公开多个 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();
    }
    

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

    【讨论】:

    • 感谢 jzheaux,抱歉回复晚了。是的,正如您所说,我已经为两个 SecurityWebFilterChains 配置了两个不同的 securityMatcher、两个 securityContext,并且我已经对它们进行了排序,以便最具体的一个成为第一个。不知道spring security支持验证jws(我用的是这个:org.jose4j.jwt.consumer),以后会考虑的。干杯!
    • @jzheaux 很抱歉造成混乱,我的意思是订单可以与 WebSecurityConfigurerAdapter 一起正常工作,但不能与 SecurityWebFilterChain 一起工作。这是我在论坛上的问题。 stackoverflow.com/questions/59471908/…
    • 我可以确认提供两个不同的 SecurityWebFilterChain bean 是有效的。似乎需要指定 securityMatcher。
    • 我也确认使用securityMatcher 是成功的关键。使用任何其他匹配器是不够的。
    • 看了ServerHttpSecurity的代码后,我可以确定securityMatcher是关键点,因为这是唯一可以在ServerHttpSecurity中设置matcher字段的方法。其他所谓的 matcher 方法只设置过滤器。代理的工作原理是它遵循SecurityWebFilterChain 的列表并尝试调用每个匹配器,一旦它获得匹配,它使用该链的过滤器,然后忽略所有其他SecurityWebFilterChain。如果不使用securityMatcher,默认匹配器匹配所有内容,导致代理只使用第一个。
    猜你喜欢
    • 2014-11-04
    • 2013-03-15
    • 2016-08-08
    • 2017-04-20
    • 2020-04-25
    • 2015-10-09
    • 2022-01-06
    • 2020-03-20
    相关资源
    最近更新 更多