2017-04-23 90 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

嗨,我認爲應該可以使用戶界面成爲資源服務器 –

相關問題