Spring security 将Spring Security与Okta OIDC JWT结合使用时出现的问题

Spring security 将Spring Security与Okta OIDC JWT结合使用时出现的问题,spring-security,jwt,openid-connect,okta,pkce,Spring Security,Jwt,Openid Connect,Okta,Pkce,[请参阅下面的更新设置以了解最新迭代] 我正在尝试使用SpringSecurity设置SpringBootREST应用程序。我们的前端正在Okta中使用OIDC对我们的用户进行身份验证,并在授权承载令牌中传递JWT。Okta正在使用PKCE 我引用了以下示例,它似乎是一个非常简单的设置: 我们的Spring Boot应用程序只是一个资源服务器,所以我想我只需要遵循第2.1节到第2.2.3节中的说明。我的应用程序中有@EnableResourceServer,我的应用程序中有以下内容。yml:

[请参阅下面的更新设置以了解最新迭代]

我正在尝试使用SpringSecurity设置SpringBootREST应用程序。我们的前端正在Okta中使用OIDC对我们的用户进行身份验证,并在授权承载令牌中传递JWT。Okta正在使用PKCE

我引用了以下示例,它似乎是一个非常简单的设置:

我们的Spring Boot应用程序只是一个资源服务器,所以我想我只需要遵循第2.1节到第2.2.3节中的说明。我的应用程序中有
@EnableResourceServer
,我的
应用程序中有以下内容。yml

spring:
  security:
    oauth2:
      resource:
        jwt:
          key-set-uri: https://dcsg-hub-dev.okta.com/oauth2/v1/keysZ?client_id=???
当我使用Postman调用我的应用程序时,我收到一个401错误:

{
    "error": "invalid_token",
    "error_description": "Invalid access token: eyJraWQ..."
}
它显示了有效的令牌,我可以通过调用Okta
userinfo
端点来验证。不幸的是,它没有给我任何其他信息

最后,我在
org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter
类中设置了断点。进入从此位置调用的代码时,在
org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager
中的代码处导致错误:

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (authentication == null) {
        throw new InvalidTokenException("Invalid token (token not found)");
    } else {
        String token = (String)authentication.getPrincipal();
        OAuth2Authentication auth = this.tokenServices.loadAuthentication(token);
        if (auth == null) {
            throw new InvalidTokenException("Invalid token: " + token);
在同一类中调用以下代码:

public OAuth2Authentication loadAuthentication(String accessTokenValue) throws AuthenticationException, InvalidTokenException {
    OAuth2AccessToken accessToken = this.tokenStore.readAccessToken(accessTokenValue);
    if (accessToken == null) {
        throw new InvalidTokenException("Invalid access token: " + accessTokenValue);
代码似乎将令牌本身用作从哈希映射检索令牌的密钥。如上所示,它使用
getPrincipal()
获取“令牌”。在我看来,它认为令牌应该是一个用户ID,它正试图使用该ID来查找用户的令牌。当值已经存在时,它使用令牌作为键来获取值,这似乎很奇怪

有什么想法吗

>>

我在这个链接中跟踪了文档:

我的
application.yml
现在看起来像这样:

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: https://dcsg-hub-dev.okta.com
          jwk-set-uri: https://dcsg-hub-dev.okta.com/oauth2/v1/keys?client_id=
这现在肯定是在调用JWK URI,而以前不是。但是,现在我得到以下错误:

Caused by: com.nimbusds.jose.proc.BadJOSEException: Signed JWT rejected: Another algorithm expected, or no matching key(s) found

我在代码中设置了断点,无论出于何种原因,Okta返回的令牌/JWT中的
kid
与Okta
jwk集uri中的任何键都不匹配,我从Okta发现此功能不受支持,您需要使用API访问管理创建自己的授权服务器。

我从Okta了解到此功能不受支持,您需要使用API访问管理创建自己的授权服务器。

您是正确的。尽管这看起来有点违反直觉,但它使用字符串accessToken来获取OAuth2AccessToken(身份验证对象)您确定要使用Spring引导自动配置吗?spring-security-oauth2-autoconfigure@EnableResourceServer是针对Spring Boot 1.5.x的,Spring Boot 2.1+将其内置到http配置中。有关更多信息,请参阅。我正在使用spring-security-oauth2-autoconfigure。当我删除@EnableResourceServer时,无效令牌错误消失了,但现在我只得到了一个普通的401。我将继续调查此事。我忘了在上面提到我们在Okta中使用PKCE。你是对的。尽管这看起来有点违反直觉,但它使用字符串accessToken来获取OAuth2AccessToken(身份验证对象)您确定要使用Spring引导自动配置吗?spring-security-oauth2-autoconfigure@EnableResourceServer是针对Spring Boot 1.5.x的,Spring Boot 2.1+将其内置到http配置中。有关更多信息,请参阅。我正在使用spring-security-oauth2-autoconfigure。当我删除@EnableResourceServer时,无效令牌错误消失了,但现在我只得到了一个普通的401。我将继续调查此事。我忘了在上面提到我们在Okta中使用PKCE。