Example usage for org.springframework.http.client.support BasicAuthorizationInterceptor BasicAuthorizationInterceptor

List of usage examples for org.springframework.http.client.support BasicAuthorizationInterceptor BasicAuthorizationInterceptor

Introduction

In this page you can find the example usage for org.springframework.http.client.support BasicAuthorizationInterceptor BasicAuthorizationInterceptor.

Prototype

public BasicAuthorizationInterceptor(@Nullable String username, @Nullable String password) 

Source Link

Document

Create a new interceptor which adds a BASIC authorization header for the given username and password.

Usage

From source file:org.springframework.cloud.netflix.eureka.http.RestTemplateTransportClientFactory.java

private RestTemplate restTemplate(String serviceUrl) {
    RestTemplate restTemplate = new RestTemplate();
    try {/* w w w .  j av a 2s  . c o m*/
        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;
}

From source file:org.springframework.boot.test.web.client.TestRestTemplate.java

private void addAuthentication(RestTemplate restTemplate, String username, String password) {
    if (username == null) {
        return;//from  ww  w .jav a  2  s  .c  om
    }
    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);
}

From source file:org.springframework.boot.web.client.RestTemplateBuilder.java

/**
 * Add HTTP basic authentication to requests. See
 * {@link BasicAuthorizationInterceptor} for details.
 * @param username the user name//from w  w w  . j ava 2  s.c om
 * @param password the password
 * @return a new builder instance
 */
public RestTemplateBuilder basicAuthorization(String username, String password) {
    return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri, this.messageConverters,
            this.requestFactory, this.uriTemplateHandler, this.errorHandler,
            new BasicAuthorizationInterceptor(username, password), this.restTemplateCustomizers,
            this.requestFactoryCustomizers, this.interceptors);
}