下面列出了怎么用org.springframework.http.client.InterceptingClientHttpRequestFactory的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Init
*/
@PostConstruct
protected void init() {
restTemplateForAuthenticationFlow = new CookieStoreRestTemplate();
cookieStore = restTemplateForAuthenticationFlow.getCookieStore();
logger.debug("Inject cookie store used in the rest template for authentication flow into the authRestTemplate so that they will match");
authRestTemplate.restTemplate.setCookieStoreAndUpdateRequestFactory(cookieStore);
List<ClientHttpRequestInterceptor> interceptors = Collections
.<ClientHttpRequestInterceptor>singletonList(new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
if (latestCsrfToken != null) {
// At the beginning of auth flow, there's no token yet
injectCsrfTokenIntoHeader(request, latestCsrfToken);
}
return execution.execute(request, body);
}
});
restTemplateForAuthenticationFlow.setRequestFactory(new InterceptingClientHttpRequestFactory(restTemplateForAuthenticationFlow.getRequestFactory(), interceptors));
}
@Override
public ClientHttpRequestFactory getRequestFactory() {
ClientHttpRequestFactory delegate = super.getRequestFactory();
if (!CollectionUtils.isEmpty(getInterceptors())) {
return new InterceptingClientHttpRequestFactory(delegate, getInterceptors());
}
else {
return delegate;
}
}
@Override
public ClientHttpRequestFactory getRequestFactory() {
ClientHttpRequestFactory delegate = super.getRequestFactory();
if (!CollectionUtils.isEmpty(getInterceptors())) {
return new InterceptingClientHttpRequestFactory(delegate, getInterceptors());
}
else {
return delegate;
}
}
/**
* Initialize the internal restTemplate instance
*/
@PostConstruct
protected void init() {
logger.debug("Create the RestTemplate instance that will be wrapped");
makeRestTemplateWithCustomObjectMapper(restTemplate);
setErrorHandlerWithLogging(restTemplate);
logger.debug("Set interceptor for authentication");
List<ClientHttpRequestInterceptor> interceptors = Collections
.<ClientHttpRequestInterceptor>singletonList(formLoginAuthenticationCsrfTokenInterceptor);
restTemplate.setRequestFactory(new InterceptingClientHttpRequestFactory(restTemplate.getRequestFactory(), interceptors));
}