Spring security oauth2 在 30 秒后未能通过颁发者验证

Spring security oauth2 fails issuer validation after 30 seconds

在反应式 spring webflux 服务中,我将端点配置为受 OAuth2 资源服务器保护。当我第一次启动服务器时,它会正确验证 Bearer 令牌,但大约 30 秒后,完全相同的请求开始失败并出现以下错误:

error="invalid_token"
error_description="This iss claim is not equal to the configured issuer"
error_uri="https://tools.ietf.org/html/rfc6750#section-3.1"

我已验证令牌有效并且 iss 声明似乎与 spring.security.oauth2.resourceserver.jwt.issuer-uri 中配置的相同。如果配置不正确,那么我将得不到有效请求。

经过仔细检查,我发现错误源于 URL 比较 iss 声明和预期的 URL,因为 InetAddress.getAddress() 匹配前 30 秒,但随后不匹配。这是使用 Azure AD 提供程序端点 https://sts.windows.net/{{tenantId}}/ 并且我已经验证 URL strings 匹配,而不是内部地址。可能是什么原因导致的,我如何在最初的 30 秒后通过有效的发行者验证令牌?谢谢。

供参考,这是我的 SecurityWebFilterChain:

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
    return http
            .csrf().disable()
            .authorizeExchange().anyExchange().authenticated()
            .and().oauth2ResourceServer().jwt().and()
            .and().build();
}

Gradle 实施包括:

org.springframework.boot:spring-boot-starter-security:2.1.0.RC1
org.springframework.boot:spring-boot-starter-webflux:2.1.0.RC1
org.springframework.security:spring-security-oauth2-resource-server:5.1.1.RELEASE
org.springframework.security:spring-security-oauth2-jose:5.1.1.RELEASE

看起来这是作为问题 #6073 in spring-security and was resolved in c70b65c 输入的。目前计划在 5.1.2.RELEASE 或 5.2.0.M1.

中解决

提交的解决方案将 URL 更改为字符串,除了删除阻塞的 DNS 查找调用之外,它还允许平等检查更加可靠。