Skip to content

Commit 83952ac

Browse files
committed
allow the BASIC auth to be not on the 1st place for the WWW_AUTHENTICATE header
Signed-off-by: Maxim Nesen <[email protected]>
1 parent de3e7fe commit 83952ac

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

Diff for: core-client/src/main/java/org/glassfish/jersey/client/authentication/BasicAuthenticator.java

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -17,8 +17,8 @@
1717
package org.glassfish.jersey.client.authentication;
1818

1919
import java.util.Base64;
20+
import java.util.List;
2021
import java.util.Locale;
21-
import java.util.logging.Level;
2222
import java.util.logging.Logger;
2323

2424
import javax.ws.rs.client.ClientRequestContext;
@@ -96,20 +96,22 @@ public void filterRequest(ClientRequestContext request) {
9696
* @throws ResponseAuthenticationException in case that basic credentials missing or are in invalid format
9797
*/
9898
public boolean filterResponseAndAuthenticate(ClientRequestContext request, ClientResponseContext response) {
99-
final String authenticate = response.getHeaders().getFirst(HttpHeaders.WWW_AUTHENTICATE);
100-
if (authenticate != null && authenticate.trim().toUpperCase(Locale.ROOT).startsWith("BASIC")) {
101-
HttpAuthenticationFilter.Credentials credentials = HttpAuthenticationFilter
102-
.getCredentials(request, defaultCredentials, HttpAuthenticationFilter.Type.BASIC);
103-
104-
if (credentials == null) {
105-
if (response.hasEntity()) {
106-
AuthenticationUtil.discardInputAndClose(response.getEntityStream());
107-
}
108-
throw new ResponseAuthenticationException(null, LocalizationMessages.AUTHENTICATION_CREDENTIALS_MISSING_BASIC());
109-
}
99+
final List<String> authHeaders = response.getHeaders().get(HttpHeaders.WWW_AUTHENTICATE);
100+
if (authHeaders == null || authHeaders.size() == 0 || authHeaders.stream()
101+
.noneMatch(h -> h != null && h.toUpperCase(Locale.ROOT).startsWith("BASIC"))) {
102+
return false;
103+
}
104+
105+
HttpAuthenticationFilter.Credentials credentials = HttpAuthenticationFilter
106+
.getCredentials(request, defaultCredentials, HttpAuthenticationFilter.Type.BASIC);
110107

111-
return HttpAuthenticationFilter.repeatRequest(request, response, calculateAuthentication(credentials));
108+
if (credentials == null) {
109+
if (response.hasEntity()) {
110+
AuthenticationUtil.discardInputAndClose(response.getEntityStream());
111+
}
112+
throw new ResponseAuthenticationException(null, LocalizationMessages.AUTHENTICATION_CREDENTIALS_MISSING_BASIC());
112113
}
113-
return false;
114+
115+
return HttpAuthenticationFilter.repeatRequest(request, response, calculateAuthentication(credentials));
114116
}
115117
}

0 commit comments

Comments
 (0)