Skip to content

Commit 80fdd47

Browse files
committed
Merge pull request #14830 from dreis2211
* pr/14830: Polish "Remove usages of BasicAuthorizationInterceptor" Remove usages of BasicAuthorizationInterceptor
2 parents 3bda199 + 1e6851c commit 80fdd47

File tree

5 files changed

+58
-33
lines changed

5 files changed

+58
-33
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5914,7 +5914,7 @@ The following code shows a typical example:
59145914

59155915
TIP: `RestTemplateBuilder` includes a number of useful methods that can be used to
59165916
quickly configure a `RestTemplate`. For example, to add BASIC auth support, you can use
5917-
`builder.basicAuthorization("user", "password").build()`.
5917+
`builder.basicAuthentication("user", "password").build()`.
59185918

59195919

59205920

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
import org.springframework.http.client.ClientHttpResponse;
5252
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
5353
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
54-
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
54+
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
5555
import org.springframework.util.Assert;
5656
import org.springframework.util.ReflectionUtils;
5757
import org.springframework.web.client.DefaultResponseErrorHandler;
@@ -172,8 +172,8 @@ private void addAuthentication(RestTemplate restTemplate, String username,
172172
interceptors = Collections.emptyList();
173173
}
174174
interceptors = new ArrayList<>(interceptors);
175-
interceptors.removeIf(BasicAuthorizationInterceptor.class::isInstance);
176-
interceptors.add(new BasicAuthorizationInterceptor(username, password));
175+
interceptors.removeIf(BasicAuthenticationInterceptor.class::isInstance);
176+
interceptors.add(new BasicAuthenticationInterceptor(username, password));
177177
restTemplate.setInterceptors(interceptors);
178178
}
179179

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
4040
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
4141
import org.springframework.http.client.SimpleClientHttpRequestFactory;
42-
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
42+
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
4343
import org.springframework.mock.env.MockEnvironment;
4444
import org.springframework.mock.http.client.MockClientHttpRequest;
4545
import org.springframework.mock.http.client.MockClientHttpResponse;
@@ -371,7 +371,7 @@ private void assertBasicAuthorizationInterceptorCredentials(
371371
"interceptors");
372372
assertThat(requestFactoryInterceptors).hasSize(1);
373373
ClientHttpRequestInterceptor interceptor = requestFactoryInterceptors.get(0);
374-
assertThat(interceptor).isInstanceOf(BasicAuthorizationInterceptor.class);
374+
assertThat(interceptor).isInstanceOf(BasicAuthenticationInterceptor.class);
375375
assertThat(interceptor).hasFieldOrPropertyWithValue("username", username);
376376
assertThat(interceptor).hasFieldOrPropertyWithValue("password", password);
377377

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper;
3434
import org.springframework.http.client.ClientHttpRequestFactory;
3535
import org.springframework.http.client.ClientHttpRequestInterceptor;
36-
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
36+
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
3737
import org.springframework.http.converter.HttpMessageConverter;
3838
import org.springframework.util.Assert;
3939
import org.springframework.util.CollectionUtils;
@@ -74,7 +74,7 @@ public class RestTemplateBuilder {
7474

7575
private final ResponseErrorHandler errorHandler;
7676

77-
private final BasicAuthorizationInterceptor basicAuthorization;
77+
private final BasicAuthenticationInterceptor basicAuthentication;
7878

7979
private final Set<RestTemplateCustomizer> restTemplateCustomizers;
8080

@@ -95,7 +95,7 @@ public RestTemplateBuilder(RestTemplateCustomizer... customizers) {
9595
this.requestFactorySupplier = null;
9696
this.uriTemplateHandler = null;
9797
this.errorHandler = null;
98-
this.basicAuthorization = null;
98+
this.basicAuthentication = null;
9999
this.restTemplateCustomizers = Collections
100100
.unmodifiableSet(new LinkedHashSet<>(Arrays.asList(customizers)));
101101
this.requestFactoryCustomizer = new RequestFactoryCustomizer();
@@ -106,7 +106,7 @@ private RestTemplateBuilder(boolean detectRequestFactory, String rootUri,
106106
Set<HttpMessageConverter<?>> messageConverters,
107107
Supplier<ClientHttpRequestFactory> requestFactorySupplier,
108108
UriTemplateHandler uriTemplateHandler, ResponseErrorHandler errorHandler,
109-
BasicAuthorizationInterceptor basicAuthorization,
109+
BasicAuthenticationInterceptor basicAuthentication,
110110
Set<RestTemplateCustomizer> restTemplateCustomizers,
111111
RequestFactoryCustomizer requestFactoryCustomizer,
112112
Set<ClientHttpRequestInterceptor> interceptors) {
@@ -116,7 +116,7 @@ private RestTemplateBuilder(boolean detectRequestFactory, String rootUri,
116116
this.requestFactorySupplier = requestFactorySupplier;
117117
this.uriTemplateHandler = uriTemplateHandler;
118118
this.errorHandler = errorHandler;
119-
this.basicAuthorization = basicAuthorization;
119+
this.basicAuthentication = basicAuthentication;
120120
this.restTemplateCustomizers = restTemplateCustomizers;
121121
this.requestFactoryCustomizer = requestFactoryCustomizer;
122122
this.interceptors = interceptors;
@@ -132,7 +132,7 @@ private RestTemplateBuilder(boolean detectRequestFactory, String rootUri,
132132
public RestTemplateBuilder detectRequestFactory(boolean detectRequestFactory) {
133133
return new RestTemplateBuilder(detectRequestFactory, this.rootUri,
134134
this.messageConverters, this.requestFactorySupplier,
135-
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
135+
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
136136
this.restTemplateCustomizers, this.requestFactoryCustomizer,
137137
this.interceptors);
138138
}
@@ -146,7 +146,7 @@ public RestTemplateBuilder detectRequestFactory(boolean detectRequestFactory) {
146146
public RestTemplateBuilder rootUri(String rootUri) {
147147
return new RestTemplateBuilder(this.detectRequestFactory, rootUri,
148148
this.messageConverters, this.requestFactorySupplier,
149-
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
149+
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
150150
this.restTemplateCustomizers, this.requestFactoryCustomizer,
151151
this.interceptors);
152152
}
@@ -182,7 +182,7 @@ public RestTemplateBuilder messageConverters(
182182
Collections.unmodifiableSet(
183183
new LinkedHashSet<HttpMessageConverter<?>>(messageConverters)),
184184
this.requestFactorySupplier, this.uriTemplateHandler, this.errorHandler,
185-
this.basicAuthorization, this.restTemplateCustomizers,
185+
this.basicAuthentication, this.restTemplateCustomizers,
186186
this.requestFactoryCustomizer, this.interceptors);
187187
}
188188

@@ -214,7 +214,7 @@ public RestTemplateBuilder additionalMessageConverters(
214214
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
215215
append(this.messageConverters, messageConverters),
216216
this.requestFactorySupplier, this.uriTemplateHandler, this.errorHandler,
217-
this.basicAuthorization, this.restTemplateCustomizers,
217+
this.basicAuthentication, this.restTemplateCustomizers,
218218
this.requestFactoryCustomizer, this.interceptors);
219219
}
220220

@@ -230,7 +230,7 @@ public RestTemplateBuilder defaultMessageConverters() {
230230
Collections.unmodifiableSet(
231231
new LinkedHashSet<>(new RestTemplate().getMessageConverters())),
232232
this.requestFactorySupplier, this.uriTemplateHandler, this.errorHandler,
233-
this.basicAuthorization, this.restTemplateCustomizers,
233+
this.basicAuthentication, this.restTemplateCustomizers,
234234
this.requestFactoryCustomizer, this.interceptors);
235235
}
236236

@@ -263,7 +263,7 @@ public RestTemplateBuilder interceptors(
263263
Assert.notNull(interceptors, "interceptors must not be null");
264264
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
265265
this.messageConverters, this.requestFactorySupplier,
266-
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
266+
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
267267
this.restTemplateCustomizers, this.requestFactoryCustomizer,
268268
Collections.unmodifiableSet(new LinkedHashSet<>(interceptors)));
269269
}
@@ -295,7 +295,7 @@ public RestTemplateBuilder additionalInterceptors(
295295
Assert.notNull(interceptors, "interceptors must not be null");
296296
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
297297
this.messageConverters, this.requestFactorySupplier,
298-
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
298+
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
299299
this.restTemplateCustomizers, this.requestFactoryCustomizer,
300300
append(this.interceptors, interceptors));
301301
}
@@ -337,7 +337,7 @@ public RestTemplateBuilder requestFactory(
337337
"RequestFactory Supplier must not be null");
338338
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
339339
this.messageConverters, requestFactorySupplier, this.uriTemplateHandler,
340-
this.errorHandler, this.basicAuthorization, this.restTemplateCustomizers,
340+
this.errorHandler, this.basicAuthentication, this.restTemplateCustomizers,
341341
this.requestFactoryCustomizer, this.interceptors);
342342
}
343343

@@ -351,7 +351,7 @@ public RestTemplateBuilder uriTemplateHandler(UriTemplateHandler uriTemplateHand
351351
Assert.notNull(uriTemplateHandler, "UriTemplateHandler must not be null");
352352
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
353353
this.messageConverters, this.requestFactorySupplier, uriTemplateHandler,
354-
this.errorHandler, this.basicAuthorization, this.restTemplateCustomizers,
354+
this.errorHandler, this.basicAuthentication, this.restTemplateCustomizers,
355355
this.requestFactoryCustomizer, this.interceptors);
356356
}
357357

@@ -365,23 +365,37 @@ public RestTemplateBuilder errorHandler(ResponseErrorHandler errorHandler) {
365365
Assert.notNull(errorHandler, "ErrorHandler must not be null");
366366
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
367367
this.messageConverters, this.requestFactorySupplier,
368-
this.uriTemplateHandler, errorHandler, this.basicAuthorization,
368+
this.uriTemplateHandler, errorHandler, this.basicAuthentication,
369369
this.restTemplateCustomizers, this.requestFactoryCustomizer,
370370
this.interceptors);
371371
}
372372

373373
/**
374374
* Add HTTP basic authentication to requests. See
375-
* {@link BasicAuthorizationInterceptor} for details.
375+
* {@link BasicAuthenticationInterceptor} for details.
376376
* @param username the user name
377377
* @param password the password
378378
* @return a new builder instance
379+
* @deprecated since 2.1.0 in favor of
380+
* {@link #basicAuthentication(String username, String password)}
379381
*/
380382
public RestTemplateBuilder basicAuthorization(String username, String password) {
383+
return basicAuthentication(username, password);
384+
}
385+
386+
/**
387+
* Add HTTP basic authentication to requests. See
388+
* {@link BasicAuthenticationInterceptor} for details.
389+
* @param username the user name
390+
* @param password the password
391+
* @return a new builder instance
392+
* @since 2.1.0
393+
*/
394+
public RestTemplateBuilder basicAuthentication(String username, String password) {
381395
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
382396
this.messageConverters, this.requestFactorySupplier,
383397
this.uriTemplateHandler, this.errorHandler,
384-
new BasicAuthorizationInterceptor(username, password),
398+
new BasicAuthenticationInterceptor(username, password),
385399
this.restTemplateCustomizers, this.requestFactoryCustomizer,
386400
this.interceptors);
387401
}
@@ -417,7 +431,7 @@ public RestTemplateBuilder customizers(
417431
"RestTemplateCustomizers must not be null");
418432
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
419433
this.messageConverters, this.requestFactorySupplier,
420-
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
434+
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
421435
Collections.unmodifiableSet(new LinkedHashSet<RestTemplateCustomizer>(
422436
restTemplateCustomizers)),
423437
this.requestFactoryCustomizer, this.interceptors);
@@ -451,7 +465,7 @@ public RestTemplateBuilder additionalCustomizers(
451465
Assert.notNull(customizers, "RestTemplateCustomizers must not be null");
452466
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
453467
this.messageConverters, this.requestFactorySupplier,
454-
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
468+
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
455469
append(this.restTemplateCustomizers, customizers),
456470
this.requestFactoryCustomizer, this.interceptors);
457471
}
@@ -465,7 +479,7 @@ public RestTemplateBuilder additionalCustomizers(
465479
public RestTemplateBuilder setConnectTimeout(Duration connectTimeout) {
466480
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
467481
this.messageConverters, this.requestFactorySupplier,
468-
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
482+
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
469483
this.restTemplateCustomizers,
470484
this.requestFactoryCustomizer.connectTimeout(connectTimeout),
471485
this.interceptors);
@@ -492,7 +506,7 @@ public RestTemplateBuilder setConnectTimeout(int connectTimeout) {
492506
public RestTemplateBuilder setReadTimeout(Duration readTimeout) {
493507
return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri,
494508
this.messageConverters, this.requestFactorySupplier,
495-
this.uriTemplateHandler, this.errorHandler, this.basicAuthorization,
509+
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
496510
this.restTemplateCustomizers,
497511
this.requestFactoryCustomizer.readTimeout(readTimeout),
498512
this.interceptors);
@@ -556,8 +570,8 @@ public <T extends RestTemplate> T configure(T restTemplate) {
556570
if (this.rootUri != null) {
557571
RootUriTemplateHandler.addTo(restTemplate, this.rootUri);
558572
}
559-
if (this.basicAuthorization != null) {
560-
restTemplate.getInterceptors().add(this.basicAuthorization);
573+
if (this.basicAuthentication != null) {
574+
restTemplate.getInterceptors().add(this.basicAuthentication);
561575
}
562576
restTemplate.getInterceptors().addAll(this.interceptors);
563577
if (!CollectionUtils.isEmpty(this.restTemplateCustomizers)) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
3535
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
3636
import org.springframework.http.client.SimpleClientHttpRequestFactory;
37-
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
37+
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
3838
import org.springframework.http.converter.HttpMessageConverter;
3939
import org.springframework.http.converter.ResourceHttpMessageConverter;
4040
import org.springframework.http.converter.StringHttpMessageConverter;
@@ -321,10 +321,21 @@ public void errorHandlerShouldApply() {
321321
}
322322

323323
@Test
324+
public void basicAuthenticationShouldApply() {
325+
RestTemplate template = this.builder.basicAuthentication("spring", "boot")
326+
.build();
327+
ClientHttpRequestInterceptor interceptor = template.getInterceptors().get(0);
328+
assertThat(interceptor).isInstanceOf(BasicAuthenticationInterceptor.class);
329+
assertThat(interceptor).extracting("username").containsExactly("spring");
330+
assertThat(interceptor).extracting("password").containsExactly("boot");
331+
}
332+
333+
@Test
334+
@Deprecated
324335
public void basicAuthorizationShouldApply() {
325336
RestTemplate template = this.builder.basicAuthorization("spring", "boot").build();
326337
ClientHttpRequestInterceptor interceptor = template.getInterceptors().get(0);
327-
assertThat(interceptor).isInstanceOf(BasicAuthorizationInterceptor.class);
338+
assertThat(interceptor).isInstanceOf(BasicAuthenticationInterceptor.class);
328339
assertThat(interceptor).extracting("username").containsExactly("spring");
329340
assertThat(interceptor).extracting("password").containsExactly("boot");
330341
}
@@ -400,11 +411,11 @@ public void customizerShouldBeAppliedAtTheEnd() {
400411
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
401412
this.builder.interceptors(this.interceptor)
402413
.messageConverters(this.messageConverter).rootUri("http://localhost:8080")
403-
.errorHandler(errorHandler).basicAuthorization("spring", "boot")
414+
.errorHandler(errorHandler).basicAuthentication("spring", "boot")
404415
.requestFactory(() -> requestFactory).customizers((restTemplate) -> {
405416
assertThat(restTemplate.getInterceptors()).hasSize(2)
406417
.contains(this.interceptor).anyMatch(
407-
(ic) -> ic instanceof BasicAuthorizationInterceptor);
418+
(ic) -> ic instanceof BasicAuthenticationInterceptor);
408419
assertThat(restTemplate.getMessageConverters())
409420
.contains(this.messageConverter);
410421
assertThat(restTemplate.getUriTemplateHandler())

0 commit comments

Comments
 (0)