Search in sources :

Example 1 with ClientHttpRequestInterceptor

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);
}
Also used : BasicAuthorizationInterceptor(org.springframework.http.client.support.BasicAuthorizationInterceptor) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor)

Example 2 with ClientHttpRequestInterceptor

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);
}
Also used : SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) Proxy(org.springframework.boot.devtools.autoconfigure.RemoteDevToolsProperties.Proxy) InetSocketAddress(java.net.InetSocketAddress) InterceptingClientHttpRequestFactory(org.springframework.http.client.InterceptingClientHttpRequestFactory) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 3 with ClientHttpRequestInterceptor

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");
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) Test(org.junit.Test)

Example 4 with ClientHttpRequestInterceptor

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"));
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ImplicitAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider) ClientHttpRequestExecution(org.springframework.http.client.ClientHttpRequestExecution) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Example 5 with ClientHttpRequestInterceptor

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);
    }
}
Also used : HttpRequest(org.springframework.http.HttpRequest) Account(android.accounts.Account) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpRequestWrapper(org.springframework.http.client.support.HttpRequestWrapper) RestTemplate(org.springframework.web.client.RestTemplate) ArrayList(java.util.ArrayList) ClientHttpRequestExecution(org.springframework.http.client.ClientHttpRequestExecution) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor)

Aggregations

ClientHttpRequestInterceptor (org.springframework.http.client.ClientHttpRequestInterceptor)10 RestTemplate (org.springframework.web.client.RestTemplate)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)4 HttpRequest (org.springframework.http.HttpRequest)3 ClientHttpRequestExecution (org.springframework.http.client.ClientHttpRequestExecution)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 URI (java.net.URI)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpMethod (org.springframework.http.HttpMethod)2 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)2 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)2 Account (android.accounts.Account)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 List (java.util.List)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Before (org.junit.Before)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1