我怎样才能使用spring社交resttemplate交换方法忽略OAuth2 Request Interceptor

时间:2015-02-10 09:20:19

标签: java spring resttemplate spring-social

API请求标头格式

.....
Authorization: KakaoAK {token}
.....

我的自定义客户端http请求拦截器

class AdminKeyHeaderOAuth2RequestInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, ClientHttpRequestExecution execution) throws IOException {
        return execution.execute(protectedResourceRequest, body);
    }
}

使用

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.setAccept(Arrays.asList(new MediaType[]{MediaType.ALL}));
headers.set("Authorization", "KakaoAK " + adminKey); //admin key를 header에 셋팅해야함

MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
param.set("limit", limit);
param.set("fromId", fromId);
param.set("order", order);

restTemplate.setInterceptors(Arrays.asList(new ClientHttpRequestInterceptor[]{new AdminKeyHeaderOAuth2RequestInterceptor()}));

ResponseEntity<KakaoIds> response = restTemplate.exchange(buildApiUri("/v1/user/ids", param).toString(), HttpMethod.GET, new HttpEntity<Object>(headers), KakaoIds.class);

但请求进程转到'org.springframework.social.oauth2.OAuth2RequestInterceptor'类的'intercep'方法并覆盖http标头。 (在我的自定义请求拦截器之后)

org.springframework.social.oauth2.OAuth2RequestInterceptor#intercep

HttpRequest protectedResourceRequest = new HttpRequestDecorator(request);
protectedResourceRequest.getHeaders().set("Authorization", oauth2Version.getAuthorizationHeaderValue(accessToken));
return execution.execute(protectedResourceRequest, body);

服务器收到请求标头格式

....
Authorization: Bearer {token}
....

我该如何解决这个问题?

0 个答案:

没有答案