spring - 如何在同一个 Spring Boot 应用程序中使用多个“JWK Set Uri”值?
问题描述
我需要使用两个不同的授权服务器(两个 Okta 实例)来验证来自单个 Spring Boot 应用程序中两个不同 Web 应用程序的身份验证令牌,该应用程序是一个后端 REST API 层。
目前我有一个资源服务器使用以下配置:
@Configuration
@EnableWebSecurity
public class ResourceServerSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.authorizeRequests().antMatchers("/public/**").permitAll()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer().jwt();
}
}
spring.security.oauth2.resourceserver.jwt.issuer-uri=https://dev-X.okta.com/oauth2/default
spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://dev-X.okta.com/oauth2/default/v1/keys
并带有依赖项spring-security-oauth2-resource-server
和spring-security-oauth2-jose
我的 Spring Boot 应用程序(版本 2.2.4.RELEASE)
我想要进入的最终状态是,根据请求中设置的自定义 HTTP 标头,我想选择我的 Spring Boot 应用程序使用哪个 Okta 实例来解码和验证 JWT 令牌。
理想情况下,我的配置文件中有两个属性,如下所示:
jwkSetUri.X=https://dev-X.okta.com/oauth2/default/v1/keys
jwtIssuerUri.X=https://dev-X.okta.com/oauth2/default
jwkSetUri.Y=https://dev-Y.okta.com/oauth2/default/v1/keys
jwtIssuerUri.Y=https://dev-Y.okta.com/oauth2/default
我应该能够使用 aRequestHeaderRequestMatcher
来匹配安全配置中的标头值。我不能锻炼的是如何使用oauth2ResourceServer
与安全配置相关的两个不同实例。
解决方案
使用弹簧靴,现在无法开箱即用。Spring Security 5.3 提供了执行此操作的功能(spring boot 2.2.6 仍然不支持 spring security 5.3)。请看以下问题:
https://github.com/spring-projects/spring-security/issues/7857
https://github.com/spring-projects/spring-security/pull/7887
通过以下我提供的链接,可以手动配置资源服务器以使用多个身份提供者。提供的链接主要用于spring boot webflux开发。有关基本的 Spring Boot Web 开发,请参阅此视频:
推荐阅读
- bash - 在 shell 脚本中使用 xml 文件中的键读取值
- java - 使用正则表达式从Java中匹配的字符串中提取组
- python - 为每个方法添加自动函数调用
- vba - VBA - 参考对象列表中的值
- excel - 使用来自另一个子的数组
- mysql - MySQL:不建议在没有服务器身份验证的情况下建立 SSL 连接。使用 SSL
- acumatica - Acumatica 和卡在运输中
- php - Laravel Horizon - 不同守卫的大门
- jquery - 迭代数组>填充选择框>从函数返回选择框作为html
- python - 如何在使用 vscode 调试时执行设置 python 虚拟环境的 shell 脚本(我需要对 launch.json 进行哪些更改)