Search in sources :

Example 1 with BasicAuthorizationInterceptor

use of org.springframework.http.client.support.BasicAuthorizationInterceptor in project mirrorgate-jira-stories-collector by BBVA.

the class Config method getRestTemplate.

@Bean(MIRRORGATE_REST_TEMPLATE)
public RestTemplate getRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
    restTemplate.getMessageConverters().add(jsonHttpMessageConverter);
    if (mirrorGateUserName.isPresent() && !mirrorGateUserName.get().isEmpty()) {
        restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(mirrorGateUserName.get(), mirrorGatePassword.get()));
    }
    return restTemplate;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) BasicAuthorizationInterceptor(org.springframework.http.client.support.BasicAuthorizationInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 2 with BasicAuthorizationInterceptor

use of org.springframework.http.client.support.BasicAuthorizationInterceptor 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 3 with BasicAuthorizationInterceptor

use of org.springframework.http.client.support.BasicAuthorizationInterceptor in project spring-boot by spring-projects.

the class SampleMethodSecurityApplicationTests method testManagementUnauthorizedAccess.

@Test
public void testManagementUnauthorizedAccess() throws Exception {
    BasicAuthorizationInterceptor basicAuthInterceptor = new BasicAuthorizationInterceptor("user", "user");
    this.restTemplate.getRestTemplate().getInterceptors().add(basicAuthInterceptor);
    try {
        ResponseEntity<String> entity = this.restTemplate.getForEntity("/beans", String.class);
        assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
    } finally {
        this.restTemplate.getRestTemplate().getInterceptors().remove(basicAuthInterceptor);
    }
}
Also used : BasicAuthorizationInterceptor(org.springframework.http.client.support.BasicAuthorizationInterceptor) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with BasicAuthorizationInterceptor

use of org.springframework.http.client.support.BasicAuthorizationInterceptor in project open-kilda by telstra.

the class ServiceConfig method buildRestTemplateWithAuth.

private RestTemplate buildRestTemplateWithAuth(String endpoint, String username, String password) {
    final RestTemplate restTemplate = new RestTemplate();
    restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(endpoint));
    restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(username, password));
    restTemplate.setErrorHandler(buildErrorHandler());
    return restTemplate;
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) BasicAuthorizationInterceptor(org.springframework.http.client.support.BasicAuthorizationInterceptor) DefaultUriBuilderFactory(org.springframework.web.util.DefaultUriBuilderFactory)

Example 5 with BasicAuthorizationInterceptor

use of org.springframework.http.client.support.BasicAuthorizationInterceptor in project mirrorgate-jira-stories-collector by BBVA.

the class Config method getJiraRestTemplate.

@Bean(JIRA_REST_TEMPLATE)
public RestTemplate getJiraRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
    restTemplate.getMessageConverters().add(jsonHttpMessageConverter);
    restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(jiraUserName, jiraPassword));
    return restTemplate;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) BasicAuthorizationInterceptor(org.springframework.http.client.support.BasicAuthorizationInterceptor) Bean(org.springframework.context.annotation.Bean)

Aggregations

BasicAuthorizationInterceptor (org.springframework.http.client.support.BasicAuthorizationInterceptor)8 RestTemplate (org.springframework.web.client.RestTemplate)5 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 Bean (org.springframework.context.annotation.Bean)2 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)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