![](/img/trans.png)
[英]How to extract claims from token in Resource Server, in Spring Boot
[英]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 controller:
@GetMapping("/user/me")
public Principal user(Principal principal) {
return principal;
}
当我调用 /user/me 端点时,我得到了 invalid_token。
我的资源服务器日志:
我的授权服务器日志:
我的代码有什么问题?
更新
问题是因为这段代码:
我遇到过同样的问题。 就我而言,我使用的是 spring 云 oauth2,Hoxton.SR4 版本,它正在工作。 所以,我改用 Hoxton.SR6 并抛出了问题。 我的授权服务器也是 Eureka 的客户端,问题的根源在于这种依赖关系。 在 Eureka Client 中有一个依赖,名为 jackson-dataformat-xml,因为它返回的 check_token 端点被转换为 xml 而不是 json。 当 RemoteTokenServices 调用 check_token 时,结果是 xml,它没有以正确的方式在 map<String,Object> 中反序列化。 如果您有多个 aud、scope 或授权,它会选择最后一个。 并且 active 属性被视为字符串。 在我的情况下,我解决了在授权服务器中排除 Eureka Client 提到的依赖项的问题,如下所示:
<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>
最后,我换了
<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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.