【问题标题】:Spring Boot Resource Server Invalid TokenSpring Boot 资源服务器无效的令牌
【发布时间】:2020-11-03 20:08:40
【问题描述】:

我正在尝试为 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。

我的资源服务器日志:

还有我的授权服务器日志:

我的代码有什么问题?

更新

问题是因为这段代码:

【问题讨论】:

    标签: java spring spring-boot oauth authorization


    【解决方案1】:

    我遇到了同样的问题。就我而言,我使用的是 spring cloud oauth2,Hoxton.SR4 版本,它正在工作。所以,我改用 Hoxton.SR6 并抛出了问题。我的授权服务器也是 Eureka 的客户端,问题的根源在于这种依赖关系。在 Eureka Client 内部有一个依赖,名为 jackson-dataformat-xml,因为它返回的 check_token 端点被转换为 xml 而不是 json。当 RemoteTokenServices 调用 check_token 并且结果是一个 xml 时,它不能以正确的方式在 map 中反序列化。如果您有多个 aud、范围或权限,它会选择最后一个。并且 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>
    

    【讨论】:

      【解决方案2】:

      终于换了

      <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);
              }
      

      【讨论】:

        猜你喜欢
        • 2019-10-03
        • 2021-12-26
        • 2018-12-26
        • 2015-06-18
        • 1970-01-01
        • 2021-06-04
        • 2016-08-17
        • 2021-07-17
        • 2018-10-13
        相关资源
        最近更新 更多