2015-09-30 48 views
3

我有一個可用的OAuth2RestTemplate客戶端(我使用的是spring-security-oauth2 2.0.7.RELEASE)。現在我想將它展示爲AsyncRestTemplate以利用ListenableFuture的異步語義。不幸的是,以下簡單的方法行不通:將AsyncRestTemplate公開爲OAuth2RestTemplate

// instantiate and configure OAuth2RestTemplate - works 
OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(...); 

// wrap sync restTemplate with AsyncRestTemplate - doesn't work 
AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate(
    new HttpComponentsAsyncClientHttpRequestFactory(), oAuth2RestTemplate); 

如何,我可以得到的OAuth2休息客戶端作爲AsyncRestTemplate我的HTTP服務?

回答

3

好吧,我能夠通過OAuth2RestTemplate中的accessToken手動設置「授權」標頭來使AsyncRestTemplate工作;這裏的Spring Java配置爲:

@Bean 
public OAuth2RestTemplate restTemplate() { 
    ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails(); 
    // configure oauth details 

    OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(details); 
    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); 

    return restTemplate; 
} 

@Bean 
public AsyncRestTemplate asyncRestTemplate(final OAuth2RestTemplate oAuth2RestTemplate) { 
    HttpComponentsAsyncClientHttpRequestFactory asyncRequestFactory = new HttpComponentsAsyncClientHttpRequestFactory() { 
     @Override 
     public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException { 
      AsyncClientHttpRequest asyncRequest = super.createAsyncRequest(uri, httpMethod); 

      OAuth2AccessToken accessToken = oAuth2RestTemplate.getAccessToken(); 
      asyncRequest.getHeaders().set("Authorization", String.format("%s %s", accessToken.getTokenType(), accessToken.getValue())); 

      return asyncRequest; 
     } 
    }; 
    return new AsyncRestTemplate(asyncRequestFactory, oAuth2RestTemplate); 
} 

我希望會有暴露配置OAuth2RestTemplate爲AsyncRestTemplate春季更簡單的方法。

2

上述工作,但我找到了一個更乾淨的方法。 註冊一個實現AsyncClientHttpRequestInterceptor

示例代碼:

private class Oauth2RequestInterceptor implements AsyncClientHttpRequestInterceptor 
{ 
    private final OAuth2RestTemplate oAuth2RestTemplate; 

    public Oauth2RequestInterceptor(OAuth2RestTemplate oAuth2RestTemplate) 
    { 
     this.oAuth2RestTemplate = oAuth2RestTemplate; 
    } 

    public ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body, 
     AsyncClientHttpRequestExecution execution) throws IOException 
    { 
     OAuth2AccessToken accessToken = oAuth2RestTemplate.getAccessToken(); 
     request.getHeaders() 
      .set("Authorization", String.format("%s %s", accessToken.getTokenType(), accessToken.getValue())); 
     return execution.executeAsync(request, body); 
    } 
} 

然後用它註冊您的AsyncRestTemplate

@Bean 
public AsyncRestTemplate asyncRestTemplate(AsyncClientHttpRequestFactory factory, OAuth2RestTemplate restTemplate) 
{ 
    AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate(factory, restTemplate); 
    asyncRestTemplate.setInterceptors(Collections.singletonList(new Oauth2RequestInterceptor(restTemplate))); 
    return asyncRestTemplate; 
}