Spring Security反应性WebFilterChainProxy仅调用单个筛选器链 [英] Spring Security Reactive WebFilterChainProxy only calling a single filter chain

查看:9
本文介绍了Spring Security反应性WebFilterChainProxy仅调用单个筛选器链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将安全性添加到基于Webflow的应用程序中,并且具有意味着我需要添加多个筛选器链的要求。然而,WebFilterChainProxy的当前实现使用Flux.filterWhen(...),如果我正确阅读文档,它将只返回链中的第一个匹配项。

鉴于上述情况,我有三个问题:-

  1. 我的反应知识非常有限,有人能确认我对filterWhen的理解是否正确吗?

  2. 如果是这样的话,有没有人能建议一种方法让多个筛选器链在新的Spring Security 5反应模型中工作?

  3. 如果我误解了filterWhen方法的工作方式,谁能建议为什么只处理我的一个筛选器链?

我将筛选器添加到链中的方式是在用@Order注释的多个配置方法中,类似于下面的代码块。

@Configration
@EnableWebFluxSecurity
public class SecurityConfig
    ...
    @Bean
    @Order(1)
    SecurityWebFilterChain chain1(ServerHttpSecurity http) {
        return http.httpBasic().disable()......
    }

    @Bean
    @Order(2)
    SecurityWebFilterChain chain2(ServerHttpSecurity http) {
        return http.httpBasic().disable()......
    }

    @Bean
    @Order(3)
    SecurityWebFilterChain chain3(ServerHttpSecurity http) {
        return http.httpBasic().disable()......
    }
    ...
}

当我调试应用程序时,我可以看到所有三个筛选器都已添加到WebFilterChainProxy,但我只得到了一个匹配的筛选器。我需要找到一种方法来返回所有匹配的筛选器。

有人能帮忙吗?

推荐答案

我最终使用ServerWebExchangeMatchers解决了这个问题。我的用例涉及在访问Spring Actuator端点时启用基本身份验证,而在其他路径上不启用身份验证。我通过以下代码实现了这一点:

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity httpSecurity) 
{
    httpSecurity
            .csrf().disable()
            .logout().disable()
            .formLogin().disable();


 httpSecurity.securityMatcher(ServerWebExchangeMatchers.pathMatchers("/actuator/**"))
            .httpBasic()
            .and()
            .authorizeExchange()
            .pathMatchers("/actuator/**")
            .hasRole(ACTUATOR_ADMIN_ROLE);

    return httpSecurity.build();
}

这篇关于Spring Security反应性WebFilterChainProxy仅调用单个筛选器链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆