Auth0-无法检索远程JWK集:读取超时 [英] Auth0 - Couldn't retrieve remote JWK set: Read timed out

查看:0
本文介绍了Auth0-无法检索远程JWK集:读取超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个问题:无法检索远程JWK集:读取超时

我使用的是带有Spring Boot 2.5.3的Java 11,

对于依赖关系:

  • Spring-SECURITY-OAuth2-Jose 5.5.1,
  • Spring-Boot-starter-OAuth2-Client
  • Spring-Boot-starter-Security

作为用户提供程序授权0。

有什么线索吗?

...
Caused by: java.lang.IllegalStateException: com.nimbusds.jose.RemoteKeySourceException: Couldn't retrieve remote JWK set: Read timed out
    at org.springframework.security.oauth2.jwt.JwtDecoderProviderConfigurationUtils.getSignatureAlgorithms(JwtDecoderProviderConfigurationUtils.java:107) ~[spring-security-oauth2-jose-5.5.1.jar:5.5.1]
    at org.springframework.security.oauth2.jwt.ReactiveJwtDecoders.withProviderConfiguration(ReactiveJwtDecoders.java:120) ~[spring-security-oauth2-jose-5.5.1.jar:5.5.1]
    at org.springframework.security.oauth2.jwt.ReactiveJwtDecoders.fromIssuerLocation(ReactiveJwtDecoders.java:100) ~[spring-security-oauth2-jose-5.5.1.jar:5.5.1]
    at org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerJwkConfiguration$JwtConfiguration.jwtDecoderByIssuerUri(ReactiveOAuth2ResourceServerJwkConfiguration.java:95) ~[spring-boot-autoconfigure-2.5.3.jar:2.5.3]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.9.jar:5.3.9]

推荐答案

我发现有时仍然会收到来自Auth0的超时。他们对JWK请求的反应似乎足够慢,需要更多的等待时间。导致我的项目失败的超时是在com.nimbusds.jose.jwk.source.RemoteJWKSet.

中定义的两个常量
public static final int DEFAULT_HTTP_CONNECT_TIMEOUT = 500;
public static final int DEFAULT_HTTP_READ_TIMEOUT = 500;

所以我提出了一个非常难看的解决方案,这个方案对我很管用,直到有人将这些常量转换为属性。

假设您正在使用Auth0指南https://auth0.com/docs/quickstart/backend/java-spring-security5/01-authorization 并且您将";jwk-set-uri:aop键添加到您的属性中,因为com.nimbusds.jose.jwk.Sourcee.RemoteJWKSet不是我可以用自己的定制实现替换的Bean,或者不能使用https://your-domain/.well-known/jwks.json";来替换超时参数,因为这些参数被传递到构造函数中,所以我必须复制生成我自己版本的所需类的东西。

解决方案

我制作了以下类的自己版本,只是从原始类复制代码并在需要的地方进行定制:

  • com.nimbusds.jose.jwk.Soure.RemoteJWKSet(自定义增加 超时常量)
    public static final int DEFAULT_HTTP_CONNECT_TIMEOUT = 1000;
    public static final int DEFAULT_HTTP_READ_TIMEOUT = 1000;
  • org.springframework.security.oauth2.jwt.JwtDecoders(改为自定义地创建我的RemoteJWKSet的自定义版本的实例)
    private static JwtDecoder withProviderConfiguration(Map<String, Object> configuration, String issuer) {
        CustomJwtDecoderProviderConfigurationUtils.validateIssuer(configuration, issuer);
        OAuth2TokenValidator<Jwt> jwtValidator = JwtValidators.createDefaultWithIssuer(issuer);
        String jwkSetUri = configuration.get("jwks_uri").toString();
        CustomRemoteJWKSet<SecurityContext> jwkSource = new CustomRemoteJWKSet<>(url(jwkSetUri));
        Set<SignatureAlgorithm> signatureAlgorithms = CustomJwtDecoderProviderConfigurationUtils
                .getSignatureAlgorithms(jwkSource);
        NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(jwkSetUri)
                .jwsAlgorithms((algs) -> algs.addAll(signatureAlgorithms)).build();
        jwtDecoder.setJwtValidator(jwtValidator);
        return jwtDecoder;
    }
  • org.springframework.security.oauth2.jwt.JwtDecoderProviderConfigurationUtils(这没有定制,但我不得不复制它,因为它在其包之外不可见)

然后我将配置中的解码器替换为自定义解码器

@Bean
public JwtDecoder jwtDecoder() {
    NimbusJwtDecoder jwtDecoder = (NimbusJwtDecoder)
            CustomJwtDecoders.fromOidcIssuerLocation(issuer);

    OAuth2TokenValidator<Jwt> audienceValidator = new AudienceValidator(audience);
    OAuth2TokenValidator<Jwt> withIssuer = JwtValidators.createDefaultWithIssuer(issuer);
    OAuth2TokenValidator<Jwt> withAudience = new DelegatingOAuth2TokenValidator<>(
        withIssuer, audienceValidator);

    jwtDecoder.setJwtValidator(withAudience);

    return jwtDecoder;
}

到目前为止,一切顺利,没有更多的暂停。

这篇关于Auth0-无法检索远程JWK集:读取超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆