2017-04-23 188 views
0

从一个ResourceServer到另一个ResourceServer进行通信时,我遇到了令牌中继问题。Spring Security OAuth2 JWT令牌中继问题

我的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 

的配置是在资源服务器2非常相似,不同之处在于它使用的是不同的clientId

这里是我如何创建资源Server1上OAuth2RestTemplate。

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

此调用需要JWT OAuth2令牌中继,但它不可能发生。

这是我从Postman RestClient调用此端点/test-relay时得到的异常。我在拨打电话的同时在授权标题中指定了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) 

我正在使用Spring Boot 1.5.2/3。我的资源服务器也是一个UI服务器,如果我使用Web浏览器打开网址,这个调用工作正常。

UPDATE-1

此问题仅发生于资源服务器是一个UI服务器与存在于它@EnableOAuth2Sso注释也即。对于没有@EnableOAuth2Sso的纯资源服务器,令牌中继工作得很好。

回答

0

您可能会受到我报告的错误https://github.com/spring-cloud/spring-cloud-security/issues/123的影响。查看此解决方法是否有帮助:

@Configuration 
public class WorkaroundConfig extends WebMvcConfigurerAdapter { 

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

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

} 
+0

此变通办法现在解决了我的问题。谢谢您的帮助。公开ui服务器作为资源服务器是否是一种好的做法? –

+0

嗨,我认为应该可以使用户界面成为资源服务器 –

相关问题