首页 > 解决方案 > 如何在同一个 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-serverspring-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与安全配置相关的两个不同实例。

标签: springspring-bootspring-securityoauth-2.0spring-security-oauth2

解决方案


使用弹簧靴,现在无法开箱即用。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 开发,请参阅此视频:

https://www.youtube.com/watch?v=ke13w8nab-k


推荐阅读