Search in sources :

Example 6 with BasicAuthorizationInterceptor

use of org.springframework.http.client.support.BasicAuthorizationInterceptor in project tutorials by eugenp.

the class RestTemplateFactory method afterPropertiesSet.

@Override
public void afterPropertiesSet() {
    HttpHost host = new HttpHost("localhost", 8082, "http");
    final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(host);
    restTemplate = new RestTemplate(requestFactory);
    restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("user1", "user1Pass"));
}
Also used : ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) HttpHost(org.apache.http.HttpHost) RestTemplate(org.springframework.web.client.RestTemplate) BasicAuthorizationInterceptor(org.springframework.http.client.support.BasicAuthorizationInterceptor)

Example 7 with BasicAuthorizationInterceptor

use of org.springframework.http.client.support.BasicAuthorizationInterceptor in project spring-cloud-netflix by spring-cloud.

the class RestTemplateTransportClientFactory method restTemplate.

private RestTemplate restTemplate(String serviceUrl) {
    RestTemplate restTemplate = new RestTemplate();
    try {
        URI serviceURI = new URI(serviceUrl);
        if (serviceURI.getUserInfo() != null) {
            String[] credentials = serviceURI.getUserInfo().split(":");
            if (credentials.length == 2) {
                restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(credentials[0], credentials[1]));
            }
        }
    } catch (URISyntaxException ignore) {
    }
    restTemplate.getMessageConverters().add(0, mappingJacksonHttpMessageConverter());
    return restTemplate;
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) BasicAuthorizationInterceptor(org.springframework.http.client.support.BasicAuthorizationInterceptor) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

BasicAuthorizationInterceptor (org.springframework.http.client.support.BasicAuthorizationInterceptor)7 RestTemplate (org.springframework.web.client.RestTemplate)4 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HttpHost (org.apache.http.HttpHost)1 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)1 ClientHttpRequestInterceptor (org.springframework.http.client.ClientHttpRequestInterceptor)1 DefaultUriBuilderFactory (org.springframework.web.util.DefaultUriBuilderFactory)1