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

jef*_*dio 4 java spring-security oauth-2.0 azure-active-directory spring-webflux

在响应式 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"
Run Code Online (Sandbox Code Playgroud)

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

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

作为参考,这是我的SecurityWebFilterChain

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

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
Run Code Online (Sandbox Code Playgroud)

jef*_*dio 7

看起来这#6073在 spring-security 中作为问题输入并在c70b65c. 目前计划在 5.1.2.RELEASE 或 5.2.0.M1 中解决。

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

  • 我为此拉了一个星期的头发。这个答案需要被点赞 1000 次。 (2认同)