【问题标题】:Multiple HttpSecurity for Different Path Matcher不同路径匹配器的多个 HttpSecurity
【发布时间】:2021-08-15 15:02:08
【问题描述】:

在我目前的系统中, 我们正在为下面提到的 Spring 安全网关使用以下功能。

 http
   .csrf().disable()
   .exceptionHandling().and()
   .httpBasic().disable()

但现在我想构建一个新的端点,它将使用 HttpBasic 安全性,那么如何使用 PathMatcher 管理这些东西?

如果我们想为 httpBasic disable 或 httpBasic enable 使用不同的配置方法。 那么它对我有什么作用呢?

当我将在同一个类中使用两个过滤器时,我将得到以下错误..

说明:

无法注册在类路径资源 [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class] 中定义的 bean 'conversionServicePostProcessor'。已在类路径资源 [org/springframework/security/config/annotation/web/reactive/WebFluxSecurityConfiguration.class] 中定义了具有该名称的 bean,并且已禁用覆盖。

行动:

考虑重命名其中一个 bean 或通过设置 spring.main.allow-bean-definition-overriding=true 来启用覆盖

【问题讨论】:

标签: java spring-boot spring-security


【解决方案1】:

Spring Security documentation 有一些示例说明如何做到这一点。

我认为你可以这样做:

@Configuration
@EnableWebFluxSecurity
static class MultiSecurityHttpConfig {

    @Order(Ordered.HIGHEST_PRECEDENCE)                                                      
    @Bean
    SecurityWebFilterChain yourEndpointHttpSecurity(ServerHttpSecurity http) {
        http
            .securityMatcher(new PathPatternParserServerWebExchangeMatcher("/your-endpoint/**"))      
            .authorizeExchange((exchanges) -> exchanges
                .anyExchange().authenticated()
            )
            .httpBasic(withDefaults());                         
        return http.build();
    }

    @Bean
    SecurityWebFilterChain webHttpSecurity(ServerHttpSecurity http) {                       
        http
            .csrf().disable()
            .exceptionHandling().and()
            .httpBasic().disable()                                                  
        return http.build();
    }

}

如果路径匹配/your-endpoint/**,则首先评估您的yourEndpointHttpSecurity Bean,然后评估您的默认webHttpSecurity

【讨论】:

    猜你喜欢
    • 2018-02-06
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 2015-10-16
    • 2016-06-02
    • 1970-01-01
    相关资源
    最近更新 更多