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
This commit is contained in:
Sam Brannen 2019-07-09 18:01:27 +02:00
parent 140e1e6532
commit f0d599493e
1 changed files with 15 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -39,6 +39,7 @@ import org.springframework.web.reactive.function.BodyExtractors;
*
* @author Rob Winch
* @author Arjen Poutsma
* @author Sam Brannen
* @since 5.0
*/
public abstract class ExchangeFilterFunctions {
@ -90,21 +91,22 @@ public abstract class ExchangeFilterFunctions {
/**
* Return a filter that applies HTTP Basic Authentication to the request
* headers via {@link HttpHeaders#setBasicAuth(String, String)}.
* @param user the user
* headers via {@link HttpHeaders#setBasicAuth(String)} and
* {@link HttpHeaders#encodeBasicAuth(String, String, Charset)}.
* @param username the username
* @param password the password
* @return the filter to add authentication headers with
* @see HttpHeaders#setBasicAuth(String, String)
* @see HttpHeaders#setBasicAuth(String, String, Charset)
* @see HttpHeaders#encodeBasicAuth(String, String, Charset)
* @see HttpHeaders#setBasicAuth(String)
*/
public static ExchangeFilterFunction basicAuthentication(String user, String password) {
public static ExchangeFilterFunction basicAuthentication(String username, String password) {
String encodedCredentials = HttpHeaders.encodeBasicAuth(username, password, null);
return (request, next) ->
next.exchange(ClientRequest.from(request)
.headers(headers -> headers.setBasicAuth(user, password))
.headers(headers -> headers.setBasicAuth(encodedCredentials))
.build());
}
/**
* Variant of {@link #basicAuthentication(String, String)} that looks up
* the {@link Credentials Credentials} in a
@ -132,7 +134,7 @@ public abstract class ExchangeFilterFunctions {
/**
* Stores user and password for HTTP basic authentication.
* Stores username and password for HTTP basic authentication.
* @deprecated as of Spring 5.1 in favor of using
* {@link HttpHeaders#setBasicAuth(String, String)} while building the request.
*/
@ -156,18 +158,18 @@ public abstract class ExchangeFilterFunctions {
}
/**
* Return a {@literal Consumer} that stores the given user and password
* Return a {@literal Consumer} that stores the given username and password
* as a request attribute of type {@code Credentials} that is in turn
* used by {@link ExchangeFilterFunctions#basicAuthentication()}.
* @param user the user
* @param username the username
* @param password the password
* @return a consumer that can be passed into
* {@linkplain ClientRequest.Builder#attributes(java.util.function.Consumer)}
* @see ClientRequest.Builder#attributes(java.util.function.Consumer)
* @see #BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE
*/
public static Consumer<Map<String, Object>> basicAuthenticationCredentials(String user, String password) {
Credentials credentials = new Credentials(user, password);
public static Consumer<Map<String, Object>> basicAuthenticationCredentials(String username, String password) {
Credentials credentials = new Credentials(username, password);
return (map -> map.put(BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE, credentials));
}