Skip to content

Commit 2b986af

Browse files
committed
BasicAuthenticationInterceptor with HttpHeaders.setBasicAuth alignment
Issue: SPR-17326
1 parent c8c0737 commit 2b986af

File tree

4 files changed

+101
-12
lines changed

4 files changed

+101
-12
lines changed

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -711,13 +711,14 @@ public Set<HttpMethod> getAllow() {
711711
/**
712712
* Set the value of the {@linkplain #AUTHORIZATION Authorization} header to
713713
* Basic Authentication based on the given username and password.
714-
* <p>Note that Basic Authentication only supports characters in the
714+
* <p>Note that this method only supports characters in the
715715
* {@link StandardCharsets#ISO_8859_1 ISO-8859-1} character set.
716716
* @param username the username
717717
* @param password the password
718718
* @throws IllegalArgumentException if either {@code user} or
719-
* {@code password} contain characters that cannot be encoded to ISO-8859-1.
719+
* {@code password} contain characters that cannot be encoded to ISO-8859-1
720720
* @since 5.1
721+
* @see #setBasicAuth(String, String, Charset)
721722
* @see <a href="https://tools.ietf.org/html/rfc7617">RFC 7617</a>
722723
*/
723724
public void setBasicAuth(String username, String password) {
@@ -730,9 +731,9 @@ public void setBasicAuth(String username, String password) {
730731
* @param username the username
731732
* @param password the password
732733
* @param charset the charset to use to convert the credentials into an octet
733-
* sequence. Defaults to {@linkplain StandardCharsets#ISO_8859_1 ISO-8859-1}
734-
* @throws IllegalArgumentException if either {@code user} or
735-
* {@code password} contain characters that cannot be encoded to ISO-8859-1.
734+
* sequence. Defaults to {@linkplain StandardCharsets#ISO_8859_1 ISO-8859-1}.
735+
* @throws IllegalArgumentException if {@code username} or {@code password}
736+
* contains characters that cannot be encoded to the given charset
736737
* @since 5.1
737738
* @see <a href="https://tools.ietf.org/html/rfc7617">RFC 7617</a>
738739
*/
@@ -746,8 +747,7 @@ public void setBasicAuth(String username, String password, @Nullable Charset cha
746747
CharsetEncoder encoder = charset.newEncoder();
747748
if (!encoder.canEncode(username) || !encoder.canEncode(password)) {
748749
throw new IllegalArgumentException(
749-
"Username or password contains characters that cannot be encoded to " +
750-
charset.displayName());
750+
"Username or password contains characters that cannot be encoded to " + charset.displayName());
751751
}
752752

753753
String credentialsString = username + ":" + password;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client.support;
18+
19+
import java.io.IOException;
20+
import java.nio.charset.Charset;
21+
22+
import org.springframework.http.HttpHeaders;
23+
import org.springframework.http.HttpRequest;
24+
import org.springframework.http.client.ClientHttpRequestExecution;
25+
import org.springframework.http.client.ClientHttpRequestInterceptor;
26+
import org.springframework.http.client.ClientHttpResponse;
27+
import org.springframework.lang.Nullable;
28+
import org.springframework.util.Assert;
29+
30+
/**
31+
* {@link ClientHttpRequestInterceptor} to apply a given HTTP Basic Authentication
32+
* username/password pair, unless a custom Authorization header has been set before.
33+
*
34+
* @author Juergen Hoeller
35+
* @since 5.1.1
36+
* @see HttpHeaders#setBasicAuth
37+
* @see HttpHeaders#AUTHORIZATION
38+
*/
39+
public class BasicAuthenticationInterceptor implements ClientHttpRequestInterceptor {
40+
41+
private final String username;
42+
43+
private final String password;
44+
45+
@Nullable
46+
private final Charset charset;
47+
48+
49+
/**
50+
* Create a new interceptor which adds Basic Authentication for the
51+
* given username and password.
52+
* @param username the username to use
53+
* @param password the password to use
54+
* @see HttpHeaders#setBasicAuth(String, String)
55+
*/
56+
public BasicAuthenticationInterceptor(String username, String password) {
57+
this(username, password, null);
58+
}
59+
60+
/**
61+
* Create a new interceptor which adds Basic Authentication for the
62+
* given username and password, encoded using the specified charset.
63+
* @param username the username to use
64+
* @param password the password to use
65+
* @param charset the charset to use
66+
* @see HttpHeaders#setBasicAuth(String, String, Charset)
67+
*/
68+
public BasicAuthenticationInterceptor(String username, String password, @Nullable Charset charset) {
69+
Assert.doesNotContain(username, ":", "Username must not contain a colon");
70+
this.username = username;
71+
this.password = password;
72+
this.charset = charset;
73+
}
74+
75+
76+
@Override
77+
public ClientHttpResponse intercept(
78+
HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
79+
80+
HttpHeaders headers = request.getHeaders();
81+
if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) {
82+
headers.setBasicAuth(this.username, this.password, this.charset);
83+
}
84+
return execution.execute(request, body);
85+
}
86+
87+
}

spring-web/src/main/java/org/springframework/http/client/support/BasicAuthorizationInterceptor.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,11 @@
3232
*
3333
* @author Phillip Webb
3434
* @since 4.3.1
35+
* @deprecated as of 5.1.1, in favor of {@link BasicAuthenticationInterceptor}
36+
* which reuses {@link org.springframework.http.HttpHeaders#setBasicAuth},
37+
* sharing its default charset ISO-8859-1 instead of UTF-8 as used here
3538
*/
39+
@Deprecated
3640
public class BasicAuthorizationInterceptor implements ClientHttpRequestInterceptor {
3741

3842
private final String username;
@@ -54,8 +58,8 @@ public BasicAuthorizationInterceptor(@Nullable String username, @Nullable String
5458

5559

5660
@Override
57-
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
58-
ClientHttpRequestExecution execution) throws IOException {
61+
public ClientHttpResponse intercept(
62+
HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
5963

6064
String token = Base64Utils.encodeToString(
6165
(this.username + ":" + this.password).getBytes(StandardCharsets.UTF_8));

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFilterFunctions.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,9 @@ public static ExchangeFilterFunction basicAuthentication(String user, String pas
115115
*/
116116
@Deprecated
117117
public static ExchangeFilterFunction basicAuthentication() {
118-
119118
return (request, next) -> {
120119
Credentials cred = (Credentials) request
121120
.attribute(BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE).orElse(null);
122-
123121
if (cred != null) {
124122
return next.exchange(ClientRequest.from(request)
125123
.headers(headers -> headers.setBasicAuth(cred.username, cred.password))

0 commit comments

Comments
 (0)