Skip to content

Commit bfd03e4

Browse files
AXIS2-6055 Restore preemptive Basic Auth support for HttpClient 5
The migration from HttpClient 4 to HttpClient 5 in Axis2 1.8 left a TODO for preemptive authentication — getPreemptiveAuthentication() was never checked, so credentials were only sent after a 401 challenge. This broke users who relied on preemptive auth in Axis2 1.7. When preemptiveAuthentication is true, set the Authorization header directly on the request using java.util.Base64 (available since Java 8). This bypasses the challenge/response flow, matching Axis2 1.7 behavior.
1 parent f8944fa commit bfd03e4

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

  • modules/transport/http/src/main/java/org/apache/axis2/transport/http/impl/httpclient5

modules/transport/http/src/main/java/org/apache/axis2/transport/http/impl/httpclient5/RequestImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,15 @@ public void enableAuthentication(HTTPAuthenticator authenticator) {
336336
}
337337
}
338338

339+
// AXIS2-6055: Preemptive authentication — send credentials on the first
340+
// request without waiting for a 401 challenge. This was supported in
341+
// Axis2 1.7 (HC 4) but the TODO was never implemented for HC 5.
342+
if (authenticator.getPreemptiveAuthentication() && username != null && password != null) {
343+
String credentials = username + ":" + password;
344+
String encoded = java.util.Base64.getEncoder().encodeToString(credentials.getBytes(java.nio.charset.StandardCharsets.UTF_8));
345+
httpRequestMethod.setHeader("Authorization", "Basic " + encoded);
346+
}
347+
339348
/* Customizing the priority Order */
340349
List schemes = authenticator.getAuthSchemes();
341350
if (schemes != null && schemes.size() > 0) {

0 commit comments

Comments
 (0)