Merge pull request #14830 from dreis2211

* pr/14830:
  Polish "Remove usages of BasicAuthorizationInterceptor"
  Remove usages of BasicAuthorizationInterceptor
pull/14843/merge
Stephane Nicoll 6 years ago
commit 80fdd47369

@ -5914,7 +5914,7 @@ The following code shows a typical example:
TIP: `RestTemplateBuilder` includes a number of useful methods that can be used to
quickly configure a `RestTemplate`. For example, to add BASIC auth support, you can use
`builder.basicAuthorization("user", "password").build()`.
`builder.basicAuthentication("user", "password").build()`.

@ -51,7 +51,7 @@ import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.client.DefaultResponseErrorHandler;
@ -172,8 +172,8 @@ public class TestRestTemplate {
interceptors = Collections.emptyList();
}
interceptors = new ArrayList<>(interceptors);
interceptors.removeIf(BasicAuthorizationInterceptor.class::isInstance);
interceptors.add(new BasicAuthorizationInterceptor(username, password));
interceptors.removeIf(BasicAuthenticationInterceptor.class::isInstance);
interceptors.add(new BasicAuthenticationInterceptor(username, password));
restTemplate.setInterceptors(interceptors);
}

@ -39,7 +39,7 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.mock.http.client.MockClientHttpRequest;
import org.springframework.mock.http.client.MockClientHttpResponse;
@ -371,7 +371,7 @@ public class TestRestTemplateTests {
"interceptors");
assertThat(requestFactoryInterceptors).hasSize(1);
ClientHttpRequestInterceptor interceptor = requestFactoryInterceptors.get(0);
assertThat(interceptor).isInstanceOf(BasicAuthorizationInterceptor.class);
assertThat(interceptor).isInstanceOf(BasicAuthenticationInterceptor.class);
assertThat(interceptor).hasFieldOrPropertyWithValue("username", username);
assertThat(interceptor).hasFieldOrPropertyWithValue("password", password);

@ -33,7 +33,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
@ -74,7 +74,7 @@ public class RestTemplateBuilder {
private final ResponseErrorHandler errorHandler;
private final BasicAuthorizationInterceptor basicAuthorization;
private final BasicAuthenticationInterceptor basicAuthentication;
private final Set<RestTemplateCustomizer> restTemplateCustomizers;
@ -95,7 +95,7 @@ public class RestTemplateBuilder {
this.requestFactorySupplier = null;
this.uriTemplateHandler = null;
this.errorHandler = null;
this.basicAuthorization = null;
this.basicAuthentication = null;
this.restTemplateCustomizers = Collections
.unmodifiableSet(new LinkedHashSet<>(Arrays.asList(customizers)));
this.requestFactoryCustomizer = new RequestFactoryCustomizer();
@ -106,7 +106,7 @@ public class RestTemplateBuilder {
Set<HttpMessageConverter<?>> messageConverters,
Supplier<ClientHttpRequestFactory> requestFactorySupplier,
UriTemplateHandler uriTemplateHandler, ResponseErrorHandler errorHandler,
BasicAuthorizationInterceptor basicAuthorization,
BasicAuthenticationInterceptor basicAuthentication,
Set<RestTemplateCustomizer> restTemplateCustomizers,
RequestFactoryCustomizer requestFactoryCustomizer,
Set<ClientHttpRequestInterceptor> interceptors) {
@ -116,7 +116,7 @@ public class RestTemplateBuilder {
this.requestFactorySupplier = requestFactorySupplier;
this.uriTemplateHandler = uriTemplateHandler;
this.errorHandler = errorHandler;
this.basicAuthorization = basicAuthorization;
this.basicAuthentication = basicAuthentication;
this.restTemplateCustomizers = restTemplateCustomizers;
this.requestFactoryCustomizer = requestFactoryCustomizer;
this.interceptors = interceptors;
@ -132,7 +132,7 @@ public class RestTemplateBuilder {
public RestTemplateBuilder detectRequestFactory(boolean detectRequestFactory) {
return new RestTemplateBuilder(detectRequestFactory, this.rootUri,
this.messageConverters, this.requestFactorySupplier,
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
this.restTemplateCustomizers, this.requestFactoryCustomizer,
this.interceptors);
}
@ -146,7 +146,7 @@ public class RestTemplateBuilder {
public RestTemplateBuilder rootUri(String rootUri) {
return new RestTemplateBuilder(this.detectRequestFactory, rootUri,
this.messageConverters, this.requestFactorySupplier,
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
this.restTemplateCustomizers, this.requestFactoryCustomizer,
this.interceptors);
}
@ -182,7 +182,7 @@ public class RestTemplateBuilder {
Collections.unmodifiableSet(
new LinkedHashSet<HttpMessageConverter<?>>(messageConverters)),
this.requestFactorySupplier, this.uriTemplateHandler, this.errorHandler,
this.basicAuthorization, this.restTemplateCustomizers,
this.basicAuthentication, this.restTemplateCustomizers,
this.requestFactoryCustomizer, this.interceptors);
}
@ -214,7 +214,7 @@ public class RestTemplateBuilder {
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
append(this.messageConverters, messageConverters),
this.requestFactorySupplier, this.uriTemplateHandler, this.errorHandler,
this.basicAuthorization, this.restTemplateCustomizers,
this.basicAuthentication, this.restTemplateCustomizers,
this.requestFactoryCustomizer, this.interceptors);
}
@ -230,7 +230,7 @@ public class RestTemplateBuilder {
Collections.unmodifiableSet(
new LinkedHashSet<>(new RestTemplate().getMessageConverters())),
this.requestFactorySupplier, this.uriTemplateHandler, this.errorHandler,
this.basicAuthorization, this.restTemplateCustomizers,
this.basicAuthentication, this.restTemplateCustomizers,
this.requestFactoryCustomizer, this.interceptors);
}
@ -263,7 +263,7 @@ public class RestTemplateBuilder {
Assert.notNull(interceptors, "interceptors must not be null");
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
this.messageConverters, this.requestFactorySupplier,
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
this.restTemplateCustomizers, this.requestFactoryCustomizer,
Collections.unmodifiableSet(new LinkedHashSet<>(interceptors)));
}
@ -295,7 +295,7 @@ public class RestTemplateBuilder {
Assert.notNull(interceptors, "interceptors must not be null");
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
this.messageConverters, this.requestFactorySupplier,
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
this.restTemplateCustomizers, this.requestFactoryCustomizer,
append(this.interceptors, interceptors));
}
@ -337,7 +337,7 @@ public class RestTemplateBuilder {
"RequestFactory Supplier must not be null");
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
this.messageConverters, requestFactorySupplier, this.uriTemplateHandler,
this.errorHandler, this.basicAuthorization, this.restTemplateCustomizers,
this.errorHandler, this.basicAuthentication, this.restTemplateCustomizers,
this.requestFactoryCustomizer, this.interceptors);
}
@ -351,7 +351,7 @@ public class RestTemplateBuilder {
Assert.notNull(uriTemplateHandler, "UriTemplateHandler must not be null");
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
this.messageConverters, this.requestFactorySupplier, uriTemplateHandler,
this.errorHandler, this.basicAuthorization, this.restTemplateCustomizers,
this.errorHandler, this.basicAuthentication, this.restTemplateCustomizers,
this.requestFactoryCustomizer, this.interceptors);
}
@ -365,23 +365,37 @@ public class RestTemplateBuilder {
Assert.notNull(errorHandler, "ErrorHandler must not be null");
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
this.messageConverters, this.requestFactorySupplier,
this.uriTemplateHandler, errorHandler, this.basicAuthorization,
this.uriTemplateHandler, errorHandler, this.basicAuthentication,
this.restTemplateCustomizers, this.requestFactoryCustomizer,
this.interceptors);
}
/**
* Add HTTP basic authentication to requests. See
* {@link BasicAuthorizationInterceptor} for details.
* {@link BasicAuthenticationInterceptor} for details.
* @param username the user name
* @param password the password
* @return a new builder instance
* @deprecated since 2.1.0 in favor of
* {@link #basicAuthentication(String username, String password)}
*/
public RestTemplateBuilder basicAuthorization(String username, String password) {
return basicAuthentication(username, password);
}
/**
* Add HTTP basic authentication to requests. See
* {@link BasicAuthenticationInterceptor} for details.
* @param username the user name
* @param password the password
* @return a new builder instance
* @since 2.1.0
*/
public RestTemplateBuilder basicAuthentication(String username, String password) {
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
this.messageConverters, this.requestFactorySupplier,
this.uriTemplateHandler, this.errorHandler,
new BasicAuthorizationInterceptor(username, password),
new BasicAuthenticationInterceptor(username, password),
this.restTemplateCustomizers, this.requestFactoryCustomizer,
this.interceptors);
}
@ -417,7 +431,7 @@ public class RestTemplateBuilder {
"RestTemplateCustomizers must not be null");
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
this.messageConverters, this.requestFactorySupplier,
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
Collections.unmodifiableSet(new LinkedHashSet<RestTemplateCustomizer>(
restTemplateCustomizers)),
this.requestFactoryCustomizer, this.interceptors);
@ -451,7 +465,7 @@ public class RestTemplateBuilder {
Assert.notNull(customizers, "RestTemplateCustomizers must not be null");
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
this.messageConverters, this.requestFactorySupplier,
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
append(this.restTemplateCustomizers, customizers),
this.requestFactoryCustomizer, this.interceptors);
}
@ -465,7 +479,7 @@ public class RestTemplateBuilder {
public RestTemplateBuilder setConnectTimeout(Duration connectTimeout) {
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
this.messageConverters, this.requestFactorySupplier,
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
this.restTemplateCustomizers,
this.requestFactoryCustomizer.connectTimeout(connectTimeout),
this.interceptors);
@ -492,7 +506,7 @@ public class RestTemplateBuilder {
public RestTemplateBuilder setReadTimeout(Duration readTimeout) {
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
this.messageConverters, this.requestFactorySupplier,
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
this.restTemplateCustomizers,
this.requestFactoryCustomizer.readTimeout(readTimeout),
this.interceptors);
@ -556,8 +570,8 @@ public class RestTemplateBuilder {
if (this.rootUri != null) {
RootUriTemplateHandler.addTo(restTemplate, this.rootUri);
}
if (this.basicAuthorization != null) {
restTemplate.getInterceptors().add(this.basicAuthorization);
if (this.basicAuthentication != null) {
restTemplate.getInterceptors().add(this.basicAuthentication);
}
restTemplate.getInterceptors().addAll(this.interceptors);
if (!CollectionUtils.isEmpty(this.restTemplateCustomizers)) {

@ -34,7 +34,7 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
@ -321,10 +321,21 @@ public class RestTemplateBuilderTests {
}
@Test
public void basicAuthenticationShouldApply() {
RestTemplate template = this.builder.basicAuthentication("spring", "boot")
.build();
ClientHttpRequestInterceptor interceptor = template.getInterceptors().get(0);
assertThat(interceptor).isInstanceOf(BasicAuthenticationInterceptor.class);
assertThat(interceptor).extracting("username").containsExactly("spring");
assertThat(interceptor).extracting("password").containsExactly("boot");
}
@Test
@Deprecated
public void basicAuthorizationShouldApply() {
RestTemplate template = this.builder.basicAuthorization("spring", "boot").build();
ClientHttpRequestInterceptor interceptor = template.getInterceptors().get(0);
assertThat(interceptor).isInstanceOf(BasicAuthorizationInterceptor.class);
assertThat(interceptor).isInstanceOf(BasicAuthenticationInterceptor.class);
assertThat(interceptor).extracting("username").containsExactly("spring");
assertThat(interceptor).extracting("password").containsExactly("boot");
}
@ -400,11 +411,11 @@ public class RestTemplateBuilderTests {
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
this.builder.interceptors(this.interceptor)
.messageConverters(this.messageConverter).rootUri("http://localhost:8080")
.errorHandler(errorHandler).basicAuthorization("spring", "boot")
.errorHandler(errorHandler).basicAuthentication("spring", "boot")
.requestFactory(() -> requestFactory).customizers((restTemplate) -> {
assertThat(restTemplate.getInterceptors()).hasSize(2)
.contains(this.interceptor).anyMatch(
(ic) -> ic instanceof BasicAuthorizationInterceptor);
(ic) -> ic instanceof BasicAuthenticationInterceptor);
assertThat(restTemplate.getMessageConverters())
.contains(this.messageConverter);
assertThat(restTemplate.getUriTemplateHandler())

Loading…
Cancel
Save