我可以在资源服务器启动时禁用颁发者验证吗?

Tho*_*ene 7 java spring-security-oauth2

我正在尝试使用 Spring Security 设置一个资源服务器,现在我想在我的机器上运行它,它必须通过 SSH 隧道才能到达令牌颁发者,因此我的应用程序最终调用的 URI 类似于http://localhost:1234/.well-known/openid-configuration. 然而,这个端点返回类似的https://my-auth-server.my-domain.com内容issuer的内容,当框架在启动期间尝试检查两个 URI 是否相等时,这会产生问题。

我已经能够追踪到JwtDecoderProviderConfigurationUtils进行此检查的位置,但我只是找不到任何挂钩来操纵它:

  • JwtDecoders不公开任何指示它不验证启动时的属性issuer源文件)。
  • JwtDecoderProviderConfigurationUtil使用它自己的私有的RestTemplate,所以我不能向它添加任何拦截器。
  • JwtDecoderProviderConfigurationUtil是包私有的,所以我无法访问它的任何方法来编写我自己的JwtDecoder.

我很高兴收到有关如何解决此问题的任何指示!我不想为了让它工作而复制一大堆代码。

小智 0

您可以创建一个自定义 JwtDecoder,其中不包括: JwtIssuerValidator

OAuth2TokenValidator<Jwt> defaultValidators = JwtValidators.createDefault()
Run Code Online (Sandbox Code Playgroud)

下面是一个示例,如果您想使用自己的发行者 URL 创建它,您想使用

OAuth2TokenValidator<Jwt> defaultValidators = JwtValidators
        .createDefaultWithIssuer("http://localhost:8081/auth/realms/CryptoInc");
Run Code Online (Sandbox Code Playgroud)

IE

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors().and()
        .authorizeRequests()
            .anyRequest().authenticated()
            .and().oauth2ResourceServer()
                .jwt()
                    .decoder(jwtTokenDecoder());
}

private JwtDecoder jwtTokenDecoder() {
    NimbusJwtDecoderJwkSupport decoder = (NimbusJwtDecoderJwkSupport)
            JwtDecoders.fromOidcIssuerLocation("http://localhost:8081/auth/realms/CryptoInc");
    OAuth2TokenValidator<Jwt> defaultValidators = JwtValidators
            .createDefault();
    OAuth2TokenValidator<Jwt> delegatingValidator = 
            new DelegatingOAuth2TokenValidator<>(defaultValidators, new CryptoJwtTokenValidator());
    decoder.setJwtValidator(delegatingValidator);
    return decoder;
}   
Run Code Online (Sandbox Code Playgroud)