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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 Rob Winch
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Sam Brannen
* @since 5.0 * @since 5.0
*/ */
public abstract class ExchangeFilterFunctions { public abstract class ExchangeFilterFunctions {
@ -90,21 +91,22 @@ public abstract class ExchangeFilterFunctions {
/** /**
* Return a filter that applies HTTP Basic Authentication to the request * Return a filter that applies HTTP Basic Authentication to the request
* headers via {@link HttpHeaders#setBasicAuth(String, String)}. * headers via {@link HttpHeaders#setBasicAuth(String)} and
* @param user the user * {@link HttpHeaders#encodeBasicAuth(String, String, Charset)}.
* @param username the username
* @param password the password * @param password the password
* @return the filter to add authentication headers with * @return the filter to add authentication headers with
* @see HttpHeaders#setBasicAuth(String, String) * @see HttpHeaders#encodeBasicAuth(String, String, Charset)
* @see HttpHeaders#setBasicAuth(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) -> return (request, next) ->
next.exchange(ClientRequest.from(request) next.exchange(ClientRequest.from(request)
.headers(headers -> headers.setBasicAuth(user, password)) .headers(headers -> headers.setBasicAuth(encodedCredentials))
.build()); .build());
} }
/** /**
* Variant of {@link #basicAuthentication(String, String)} that looks up * Variant of {@link #basicAuthentication(String, String)} that looks up
* the {@link Credentials Credentials} in a * 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 * @deprecated as of Spring 5.1 in favor of using
* {@link HttpHeaders#setBasicAuth(String, String)} while building the request. * {@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 * as a request attribute of type {@code Credentials} that is in turn
* used by {@link ExchangeFilterFunctions#basicAuthentication()}. * used by {@link ExchangeFilterFunctions#basicAuthentication()}.
* @param user the user * @param username the username
* @param password the password * @param password the password
* @return a consumer that can be passed into * @return a consumer that can be passed into
* {@linkplain ClientRequest.Builder#attributes(java.util.function.Consumer)} * {@linkplain ClientRequest.Builder#attributes(java.util.function.Consumer)}
* @see ClientRequest.Builder#attributes(java.util.function.Consumer) * @see ClientRequest.Builder#attributes(java.util.function.Consumer)
* @see #BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE * @see #BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE
*/ */
public static Consumer<Map<String, Object>> basicAuthenticationCredentials(String user, String password) { public static Consumer<Map<String, Object>> basicAuthenticationCredentials(String username, String password) {
Credentials credentials = new Credentials(user, password); Credentials credentials = new Credentials(username, password);
return (map -> map.put(BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE, credentials)); return (map -> map.put(BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE, credentials));
} }