Spring 引导资源服务器无效令牌

Spring Boot Resource Server Invalid Token

我正在尝试为 Spring 项目配置 OAuth2。我使用了jdbc认证,我的授权服务器和资源服务器是两个独立的API。我现在的问题是微服务。我正在尝试使用此共享授权服务器来验证微服务。我可以从令牌端点获得 access_token。

我可以从 check_token 端点检查 access_token。

我的资源服务器配置:

@SpringBootApplication
@EnableCircuitBreaker
@EnableDiscoveryClient
@EnableResourceServer
public class ProductApiServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductApiServiceApplication.class, args);
    }
    
}

和application.yml:

security:
  oauth2:
    client:  
      client-id: saba-product-api-service
      client-secret: secret123 
    resource:
      id: saba-product-api-service
      token-info-uri: http://localhost:9999/uaa/oauth/check_token

和 REST 控制器:

    @GetMapping("/user/me")
    public Principal user(Principal principal) {
        return principal;
    } 

当我调用 /user/me 端点时,我得到 invalid_token.

我的资源服务器日志:

我的授权服务器日志:

我的代码有什么问题?

更新

问题是因为这段代码:

最后,我更换了

<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth2</artifactId>
    <version>2.3.4.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth2</artifactId>
    <version>2.5.0.RELEASE</version>
</dependency>
        // gh-838
        if (map.containsKey("active") && !"true".equals(String.valueOf(map.get("active")))) {
            logger.debug("check_token returned active attribute: " + map.get("active"));
            throw new InvalidTokenException(accessToken);
        }

我遇到了同样的问题。就我而言,我使用的是 spring cloud oauth2,Hoxton.SR4 版本并且它正在运行。因此,我更改为 Hoxton.SR6,并引发了问题。我的授权服务器也是 Eureka 的客户端,问题的起因是这种依赖性。 Eureka Client 中有一个依赖项,名为 jackson-dataformat-xml,因为它 check_token 端点的 return 被转换为 xml 而不是 json。当 RemoteTokenServices 调用 check_token 并且结果是 xml 时,它无法在 map 中以正确的方式反序列化。如果您有多个审核、范围或权限,它会选择最后一个。并且 active 属性被视为字符串。就我而言,我解决了在授权服务器中排除 Eureka 客户端提到的依赖项的问题,如下所示:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </exclusion>
    </exclusions>
</dependency>