0

Spring 安全文档https://docs.spring.io/spring-security/reference/servlet/oauth2/resource-server/jwt.html#oauth2resourceserver-jwt-timeouts 指出:

默认情况下,资源服务器使用 30 秒的连接和套接字超时来与授权服务器进行协调。

我通过以下方式创建了 JwtDecoder:

@Bean
JwtDecoder jwtDecoder() {
    NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(jwkSetUri).build();
    return jwtDecoder;
}

jwkSetUri 设置为一些不存在的 ip。现在,向我的资源服务器发出请求会超时(如预期的那样)并引发以下异常:

An error occurred while attempting to decode the Jwt: Couldn't retrieve remote JWK set: org.springframework.web.client.ResourceAccessException: I/O error on GET request: Connect to jwkSetUri  [jwkSetUri ] failed: connect timed out; nested exception is org.apache.http.conn.ConnectTimeoutException: Connect to jwkSetUri  failed: connect timed out

但是,引发异常的时间与文档中描述的不匹配。当我在 Windows 上运行应用程序时,大约需要 20 秒。当我在 Linux 上运行时,大约需要 2-3 分钟。看起来它取决于操作系统。但是,当我手动设置超时如下:

@Bean
public JwtDecoder jwtDecoder(RestTemplateBuilder builder) {
    RestOperations rest = builder
            .setConnectTimeout(Duration.ofSeconds(5))
            .setReadTimeout(Duration.ofSeconds(5))
            .build();

    NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(jwkSetUri).restOperations(rest).build();
    return jwtDecoder;
}

然后正如预期的那样,我在 5 秒后超时。我是否遗漏了什么或文档中给出的默认值不正确?

4

0 回答 0