Skip to content

Commit f0d5994

Browse files
committed
Cache encoded BASIC credentials in ExchangeFilterFunctions
Prior to this commit, the Basic Authentication credentials were encoded for each request in ExchangeFilterFunctions.basicAuthentication(String, String). This commit addresses this minor performance issue by encoding the credentials prior to the creation of the lambda expression returned by ExchangeFilterFunctions.basicAuthentication(String, String). Closes gh-23256
1 parent 140e1e6 commit f0d5994

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -39,6 +39,7 @@
3939
*
4040
* @author Rob Winch
4141
* @author Arjen Poutsma
42+
* @author Sam Brannen
4243
* @since 5.0
4344
*/
4445
public abstract class ExchangeFilterFunctions {
@@ -90,21 +91,22 @@ public static ExchangeFilterFunction statusError(Predicate<HttpStatus> statusPre
9091

9192
/**
9293
* Return a filter that applies HTTP Basic Authentication to the request
93-
* headers via {@link HttpHeaders#setBasicAuth(String, String)}.
94-
* @param user the user
94+
* headers via {@link HttpHeaders#setBasicAuth(String)} and
95+
* {@link HttpHeaders#encodeBasicAuth(String, String, Charset)}.
96+
* @param username the username
9597
* @param password the password
9698
* @return the filter to add authentication headers with
97-
* @see HttpHeaders#setBasicAuth(String, String)
98-
* @see HttpHeaders#setBasicAuth(String, String, Charset)
99+
* @see HttpHeaders#encodeBasicAuth(String, String, Charset)
100+
* @see HttpHeaders#setBasicAuth(String)
99101
*/
100-
public static ExchangeFilterFunction basicAuthentication(String user, String password) {
102+
public static ExchangeFilterFunction basicAuthentication(String username, String password) {
103+
String encodedCredentials = HttpHeaders.encodeBasicAuth(username, password, null);
101104
return (request, next) ->
102105
next.exchange(ClientRequest.from(request)
103-
.headers(headers -> headers.setBasicAuth(user, password))
106+
.headers(headers -> headers.setBasicAuth(encodedCredentials))
104107
.build());
105108
}
106109

107-
108110
/**
109111
* Variant of {@link #basicAuthentication(String, String)} that looks up
110112
* the {@link Credentials Credentials} in a
@@ -132,7 +134,7 @@ public static ExchangeFilterFunction basicAuthentication() {
132134

133135

134136
/**
135-
* Stores user and password for HTTP basic authentication.
137+
* Stores username and password for HTTP basic authentication.
136138
* @deprecated as of Spring 5.1 in favor of using
137139
* {@link HttpHeaders#setBasicAuth(String, String)} while building the request.
138140
*/
@@ -156,18 +158,18 @@ public Credentials(String username, String password) {
156158
}
157159

158160
/**
159-
* Return a {@literal Consumer} that stores the given user and password
161+
* Return a {@literal Consumer} that stores the given username and password
160162
* as a request attribute of type {@code Credentials} that is in turn
161163
* used by {@link ExchangeFilterFunctions#basicAuthentication()}.
162-
* @param user the user
164+
* @param username the username
163165
* @param password the password
164166
* @return a consumer that can be passed into
165167
* {@linkplain ClientRequest.Builder#attributes(java.util.function.Consumer)}
166168
* @see ClientRequest.Builder#attributes(java.util.function.Consumer)
167169
* @see #BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE
168170
*/
169-
public static Consumer<Map<String, Object>> basicAuthenticationCredentials(String user, String password) {
170-
Credentials credentials = new Credentials(user, password);
171+
public static Consumer<Map<String, Object>> basicAuthenticationCredentials(String username, String password) {
172+
Credentials credentials = new Credentials(username, password);
171173
return (map -> map.put(BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE, credentials));
172174
}
173175

0 commit comments

Comments
 (0)