diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BasicAuthorizationProvider.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BasicAuthorizationProvider.java
index 031470e00e5..ebdf21b49b4 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BasicAuthorizationProvider.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BasicAuthorizationProvider.java
@@ -17,13 +17,16 @@
package org.apache.logging.log4j.core.util;
import java.net.URLConnection;
+import java.nio.charset.Charset;
+import java.util.Base64;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.status.StatusLogger;
-import org.apache.logging.log4j.util.Base64Util;
import org.apache.logging.log4j.util.LoaderUtil;
import org.apache.logging.log4j.util.PropertiesUtil;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
/**
* Provides the Basic Authorization header to a request.
*/
@@ -35,6 +38,11 @@ public class BasicAuthorizationProvider implements AuthorizationProvider {
public static final String CONFIG_USER_NAME = "log4j2.configurationUserName";
public static final String CONFIG_PASSWORD = "log4j2.configurationPassword";
public static final String PASSWORD_DECRYPTOR = "log4j2.passwordDecryptor";
+ /*
+ * Properties used to specify the encoding in HTTP Basic Authentication
+ */
+ private static final String BASIC_AUTH_ENCODING = "log4j2.configurationAuthorizationEncoding";
+ private static final String SPRING_BASIC_AUTH_ENCODING = "logging.auth.encoding";
private static final Logger LOGGER = StatusLogger.getLogger();
@@ -47,6 +55,11 @@ public BasicAuthorizationProvider(final PropertiesUtil props) {
() -> props.getStringProperty(CONFIG_PASSWORD));
final String decryptor = props.getStringProperty(PREFIXES, AUTH_PASSWORD_DECRYPTOR,
() -> props.getStringProperty(PASSWORD_DECRYPTOR));
+ // Password encoding
+ Charset passwordCharset = props.getCharsetProperty(BASIC_AUTH_ENCODING);
+ if (passwordCharset == null) {
+ props.getCharsetProperty(SPRING_BASIC_AUTH_ENCODING, UTF_8);
+ }
if (decryptor != null) {
try {
final Object obj = LoaderUtil.newInstanceOf(decryptor);
@@ -58,7 +71,13 @@ public BasicAuthorizationProvider(final PropertiesUtil props) {
}
}
if (userName != null && password != null) {
- authString = "Basic " + Base64Util.encode(userName + ":" + password);
+ /*
+ * https://datatracker.ietf.org/doc/html/rfc7617#appendix-B
+ *
+ * If the user didn't specify a charset to use, we fallback to UTF-8
+ */
+ authString = "Basic "
+ + Base64.getEncoder().encodeToString((userName + ":" + password).getBytes(passwordCharset));
}
}
diff --git a/src/changelog/.2.x.x/change_basic_auth_encoding.xml b/src/changelog/.2.x.x/change_basic_auth_encoding.xml
new file mode 100644
index 00000000000..82b2abc303d
--- /dev/null
+++ b/src/changelog/.2.x.x/change_basic_auth_encoding.xml
@@ -0,0 +1,10 @@
+
+