You need to enable JavaScript to run this app. Spring Webflux Security多租户资源服务器按REST路径配置独立Issuer咨询
优惠活动
大模型
产品
解决方案
定价
更多

Spring Webflux Security多租户资源服务器按REST路径配置独立Issuer咨询

实现方案

你现在的问题是全局配置了支持两个issuer的认证解析器,所以所有受保护路径都会放行两个issuer签发的令牌。要实现路径和issuer的绑定,有两种常用实现方案:


方案1:自定义路径匹配的ReactiveAuthenticationManagerResolver(改动最小,推荐)

自己实现认证管理器的解析逻辑,根据当前请求的路径返回对应issuer的认证管理器即可,代码示例如下:

import org.springframework.web.util.pattern.PathPatternParser
import org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder
import org.springframework.security.oauth2.server.resource.authentication.JwtReactiveAuthenticationManager
import reactor.core.publisher.Mono

@Bean
fun securityConfig(http: ServerHttpSecurity): SecurityWebFilterChain? {
    // 构造两个issuer各自对应的认证管理器
    val issuer1AuthManager = JwtReactiveAuthenticationManager(
        NimbusReactiveJwtDecoder.withJwkSetUri("http://localhost:8081/auth/realms/realm1/protocol/openid-connect/certs")
            .issuer("http://localhost:8081/auth/realms/realm1") // 额外校验iss字段,可选但推荐
            .build()
    )
    val issuer2AuthManager = JwtReactiveAuthenticationManager(
        NimbusReactiveJwtDecoder.withJwkSetUri("http://localhost:8082/auth/realms/realm2/protocol/openid-connect/certs")
            .issuer("http://localhost:8082/auth/realms/realm2")
            .build()
    )
    val pathPatternParser = PathPatternParser.defaultInstance
    val path1Pattern = pathPatternParser.parse("/path1/**") // 按需调整匹配规则,要精确匹配就去掉/**
    val path2Pattern = pathPatternParser.parse("/path2/**")

    http.authorizeExchange()
        .pathMatchers("/path1/**", "/path2/**").authenticated()
        .and()
        .oauth2ResourceServer { rs ->
            rs.authenticationManagerResolver { exchange ->
                val requestPath = exchange.request.path
                // 按请求路径返回对应认证管理器
                when {
                    path1Pattern.matches(requestPath) -> Mono.just(issuer1AuthManager)
                    path2Pattern.matches(requestPath) -> Mono.just(issuer2AuthManager)
                    else -> Mono.empty()
                }
            }
        }
    return http.build()
}

方案2:拆分多个SecurityWebFilterChain(适合规则差异大的场景)

如果不同路径的安全规则差异很大,可以拆分多个独立的过滤器链,分别绑定对应路径和issuer,配置完全隔离:

import org.springframework.boot.autoconfigure.security.SecurityProperties
import org.springframework.core.annotation.Order
import org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher

@Bean
@Order(1) // 优先级更高,优先匹配path1规则
fun path1SecurityConfig(http: ServerHttpSecurity): SecurityWebFilterChain {
    http.securityMatcher(PathPatternParserServerWebExchangeMatcher("/path1/**"))
        .authorizeExchange().anyExchange().authenticated()
        .and()
        .oauth2ResourceServer { rs ->
            rs.authenticationManagerResolver(
                JwtIssuerReactiveAuthenticationManagerResolver("http://localhost:8081/auth/realms/realm1")
            )
        }
    return http.build()
}

@Bean
@Order(2)
fun path2SecurityConfig(http: ServerHttpSecurity): SecurityWebFilterChain {
    http.securityMatcher(PathPatternParserServerWebExchangeMatcher("/path2/**"))
        .authorizeExchange().anyExchange().authenticated()
        .and()
        .oauth2ResourceServer { rs ->
            rs.authenticationManagerResolver(
                JwtIssuerReactiveAuthenticationManagerResolver("http://localhost:8082/auth/realms/realm2")
            )
        }
    return http.build()
}

// 其他路径默认规则,按需调整
@Bean
@Order(3)
fun defaultSecurityConfig(http: ServerHttpSecurity): SecurityWebFilterChain {
    http.authorizeExchange()
        .anyExchange().permitAll()
        .and()
        .csrf().disable()
    return http.build()
}

注意事项

  • 两种方案都可以按需调整路径匹配规则,精确匹配就写完整路径,需要匹配所有子路径就加/**后缀
  • 方案1适合当前只有路径-issuer绑定差异的场景,改动量最小;方案2扩展性更强,后续不同路径要加权限校验、匿名访问等规则时互不影响

内容的提问来源于stack exchange,提问作者Mike

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 04:06:03