use of org.springframework.http.client.ClientHttpRequestInterceptor in project spring-boot by spring-projects.
the class TestRestTemplate method addAuthentication.
private void addAuthentication(RestTemplate restTemplate, String username, String password) {
if (username == null) {
return;
}
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
if (interceptors == null) {
interceptors = Collections.emptyList();
}
interceptors = new ArrayList<>(interceptors);
Iterator<ClientHttpRequestInterceptor> iterator = interceptors.iterator();
while (iterator.hasNext()) {
if (iterator.next() instanceof BasicAuthorizationInterceptor) {
iterator.remove();
}
}
interceptors.add(new BasicAuthorizationInterceptor(username, password));
restTemplate.setInterceptors(interceptors);
}
use of org.springframework.http.client.ClientHttpRequestInterceptor in project spring-boot by spring-projects.
the class RemoteClientConfiguration method clientHttpRequestFactory.
@Bean
public ClientHttpRequestFactory clientHttpRequestFactory() {
List<ClientHttpRequestInterceptor> interceptors = Arrays.asList(getSecurityInterceptor());
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy = this.properties.getRemote().getProxy();
if (proxy.getHost() != null && proxy.getPort() != null) {
requestFactory.setProxy(new java.net.Proxy(Type.HTTP, new InetSocketAddress(proxy.getHost(), proxy.getPort())));
}
return new InterceptingClientHttpRequestFactory(requestFactory, interceptors);
}
use of org.springframework.http.client.ClientHttpRequestInterceptor in project spring-boot by spring-projects.
the class RestTemplateBuilderTests method basicAuthorizationShouldApply.
@Test
public void basicAuthorizationShouldApply() throws Exception {
RestTemplate template = this.builder.basicAuthorization("spring", "boot").build();
ClientHttpRequestInterceptor interceptor = template.getInterceptors().get(0);
assertThat(interceptor).isInstanceOf(BasicAuthorizationInterceptor.class);
assertThat(interceptor).extracting("username").containsExactly("spring");
assertThat(interceptor).extracting("password").containsExactly("boot");
}
use of org.springframework.http.client.ClientHttpRequestInterceptor in project spring-security-oauth by spring-projects.
the class ImplicitProviderTests method testPostForAutomaticApprovalToken.
@Test
@OAuth2ContextConfiguration(resource = AutoApproveImplicit.class, initialize = false)
public void testPostForAutomaticApprovalToken() throws Exception {
final ImplicitAccessTokenProvider implicitProvider = new ImplicitAccessTokenProvider();
implicitProvider.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList(new ClientHttpRequestInterceptor() {
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
ClientHttpResponse result = execution.execute(request, body);
latestHeaders = result.getHeaders();
return result;
}
}));
context.setAccessTokenProvider(implicitProvider);
context.getAccessTokenRequest().setCookie(cookie);
assertNotNull(context.getAccessToken());
assertTrue("Wrong location header: " + latestHeaders.getLocation().getFragment(), latestHeaders.getLocation().getFragment().contains("scope=read write trust"));
}
use of org.springframework.http.client.ClientHttpRequestInterceptor in project ETSMobile-Android2 by ApplETS.
the class MonETSNotificationsRequest method loadDataFromNetwork.
@Override
public MonETSNotificationList loadDataFromNetwork() throws Exception {
accountManager = AccountManager.get(context);
Account[] accounts = accountManager.getAccountsByType(Constants.ACCOUNT_TYPE);
if (accounts.length > 0) {
authToken = accountManager.peekAuthToken(accounts[0], Constants.AUTH_TOKEN_TYPE);
}
String url = context.getString(R.string.portail_api_base_url);
if (onlyNewNotifs) {
url += "/api/notification/dossier/1";
} else {
url += "/api/notification";
}
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
requestWrapper.getHeaders().set("Cookie", authToken);
return execution.execute(requestWrapper, body);
}
};
RestTemplate restTemplate = getRestTemplate();
List<ClientHttpRequestInterceptor> list = new ArrayList<ClientHttpRequestInterceptor>();
list.add(interceptor);
restTemplate.setInterceptors(list);
try {
return restTemplate.getForObject(url, MonETSNotificationList.class);
} catch (HttpClientErrorException e) {
if (e.getStatusCode().value() == 401) {
if (accounts.length > 0) {
accountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);
authToken = accountManager.blockingGetAuthToken(accounts[0], Constants.AUTH_TOKEN_TYPE, true);
interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
requestWrapper.getHeaders().set("Cookie", authToken);
return execution.execute(requestWrapper, body);
}
};
list.clear();
list.add(interceptor);
restTemplate.setInterceptors(list);
}
}
} finally {
return restTemplate.getForObject(url, MonETSNotificationList.class);
}
}
Aggregations