From f0d599493e5390e00ee14cbe6a66397a14459607 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 9 Jul 2019 18:01:27 +0200 Subject: [PATCH] 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 --- .../client/ExchangeFilterFunctions.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFilterFunctions.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFilterFunctions.java index b545f9f875..baeb77a9c8 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFilterFunctions.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFilterFunctions.java @@ -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> basicAuthenticationCredentials(String user, String password) { - Credentials credentials = new Credentials(user, password); + public static Consumer> basicAuthenticationCredentials(String username, String password) { + Credentials credentials = new Credentials(username, password); return (map -> map.put(BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE, credentials)); }