【发布时间】:2021-10-11 18:19:30
【问题描述】:
我阅读了 this post 关于在 Spring Security 流程中使用多个 JWT 解码器的文章,这似乎很容易,除了我使用的是 Spring Webflux 而不是 Spring WebMVC ,它有方便的WebSecurityConfigurerAdapter,您可以扩展它以添加多个AuthenticationProvider 实例。使用 Webflux,您不再需要扩展某些类来配置安全性。
那么在尝试使用 Webflux 复制此内容时有什么问题? This 。正如您所读到的那样,Webflux 不使用 AuthenticationProvider ,您必须声明一个 ReactiveAuthenticationManager 。问题是我不知道如何让 Spring 使用多个身份验证管理器,每个身份验证管理器都使用自己的ReactiveJwtDecoder。
我的第一个身份验证管理器将是 spring 使用此属性自动创建的一个:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: ${scacap.auth0.issuer}
我的第二个身份验证管理器将是我在安全@Configuration 中声明的自定义管理器:
@Configuration
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
@EnableConfigurationProperties(JwkProperties::class)
internal class SecurityConfiguration {
@Bean
fun securityFilter(
http: ServerHttpSecurity,
scalableAuthenticationManager: JwtReactiveAuthenticationManager
): SecurityWebFilterChain {
http.csrf().disable()
.authorizeExchange()
.anyExchange().authenticated()
.and()
.oauth2ResourceServer().jwt()
.jwtAuthenticationConverter(Auth0AuthenticationConverter())
return http.build()
}
@Bean
fun customAuthenticationManager(jwkProperties: JwkProperties): JwtReactiveAuthenticationManager {
val decoder = NimbusReactiveJwtDecoder.withJwkSource { Flux.fromIterable(jwkProperties.jwkSet.keys) }.build()
return JwtReactiveAuthenticationManager(decoder).also {
it.setJwtAuthenticationConverter(ScalableAuthenticationConverter())
}
}
}
【问题讨论】:
-
要么在属性中指定所有配置,要么在自定义 HttpSecurity 配置中覆盖。您不能在属性中指定内容(使用 springs 自动配置),然后创建一个完整的自定义配置并期望两者都能正常工作。
-
@Toerktumlare 是真的。阅读您的评论后,我专注于使其与两个手动实例化的解码器一起工作。如果您对我是如何做到的感到好奇,您可以查看我的答案:)
标签: spring-boot spring-security spring-webflux