首页 > 解决方案 > 我可以在资源服务器启动时禁用颁发者验证吗?

问题描述

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

我已经能够将其追踪JwtDecoderProviderConfigurationUtils到此检查发生的位置,但我找不到任何挂钩来操纵它:

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

标签: javaspring-security-oauth2

解决方案


您可以创建一个不包含以下内容的自定义 JwtDecoder:JwtIssuerValidator

OAuth2TokenValidator<Jwt> defaultValidators = JwtValidators.createDefault()

下面是一个例子,如果你想用你自己的发行者 URL 创建它,你想验证,即使用

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

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;
}   

推荐阅读