简体   繁体   English

Spring Security OAuth2 JWT令牌中继问题

[英]Spring Security OAuth2 JWT Token Relay Issue

I am facing issue with Token Relay while communicating from one ResourceServer to another ResourceServer. 从一个ResourceServer与另一个ResourceServer通信时,令牌中继出现问题。

My AuthServer is based on Dave Sayer's sample and this is the application.yml for resource server1. 我的AuthServer基于Dave Sayer的示例,这是资源server1的application.yml。

security:
 user:
    password: none
  oauth2:
    client:
      accessTokenUri: http://localhost:9999/uaa/oauth/token
      userAuthorizationUri: http://localhost:9999/uaa/oauth/authorize
      clientId: trusted
      clientSecret: secret

The config is very similar in resource server2, except that it is using a different clientId 该配置在资源server2中非常相似,只不过它使用的是不同的clientId

Here is how i am creating the OAuth2RestTemplate in resource server1. 这是我在资源server1中创建OAuth2RestTemplate的方法。

@LoadBalanced
@Bean
@Autowired
public OAuth2RestTemplate loadBalancedOauth2RestTemplate(OAuth2ClientContext oauth2ClientContext,
                                                         OAuth2ProtectedResourceDetails details) {
    return new OAuth2RestTemplate(details, oauth2ClientContext);
}

This call requires JWT OAuth2 Token Relay, but its not happening probably. 此调用需要JWT OAuth2令牌中继,但可能不会发生。

@GetMapping("/test-relay")
public String fetchMyProfile2() {
    final ResponseEntity<String> forEntity = oauthRestTemplate.getForEntity("http://my-oauth/users/me", String.class);
    final String body = forEntity.getBody();
    System.out.println("body = " + body);
    return body;
}

This is the exception i get while invoking this endpoint /test-relay from Postman RestClient. 这是我从Postman RestClient调用此端点/test-relay得到的异常。 I am specifying JWT Token in Authorization Header while making the call. 我在拨打电话时在“授权标题”中指定JWT令牌。

org.springframework.security.oauth2.client.resource.UserRedirectRequiredException: A redirect is required to get the users approval
at org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.getRedirectForAuthorization(AuthorizationCodeAccessTokenProvider.java:359)
at org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.obtainAccessToken(AuthorizationCodeAccessTokenProvider.java:205)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:148)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:121)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:221)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:173)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.createRequest(OAuth2RestTemplate.java:105)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:648)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:128)

I am using Spring Boot 1.5.2/3. 我正在使用Spring Boot 1.5.2 / 3。 My Resource Server is a UI Server as well, and this call works fine if i use Web Browser to hit the url. 我的资源服务器也是UI服务器,如果我使用Web浏览器访问URL,则此调用可以正常工作。

UPDATE-1 UPDATE-1

This issue only happens for Resource Server that is a UI server too ie with @EnableOAuth2Sso annotation present on it. 此问题也只会发生在也是UI服务器的资源服务器上,即上面带有@EnableOAuth2Sso批注。 For a pure Resource Server that does not have @EnableOAuth2Sso , token relay works perfectly fine. 对于没有@EnableOAuth2Sso的纯资源服务器,令牌中继工作得很好。

You might be affected by the bug I reported https://github.com/spring-cloud/spring-cloud-security/issues/123 . 您可能会受到我报告https://github.com/spring-cloud/spring-cloud-security/issues/123的错误的影响。 See if this workaround helps : 查看此变通办法是否有帮助:

@Configuration
public class WorkaroundConfig extends WebMvcConfigurerAdapter {

    @Autowired
    @Qualifier("tokenRelayRequestInterceptor")
    HandlerInterceptor handlerInterceptor;

    @Override
    public void addInterceptors (InterceptorRegistry registry) {
        registry.addInterceptor(handlerInterceptor);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM