Skip to content

Commit 101b42d

Browse files
committed
[java] Add support for basic auth in JDK client
Related to #11068
1 parent fcc4df1 commit 101b42d

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.openqa.selenium.remote.http.jdk;
1919

2020
import com.google.auto.service.AutoService;
21+
2122
import org.openqa.selenium.Credentials;
2223
import org.openqa.selenium.TimeoutException;
2324
import org.openqa.selenium.UsernameAndPassword;
@@ -78,9 +79,23 @@ public class JdkHttpClient implements HttpClient {
7879
.followRedirects(ALWAYS);
7980

8081
Credentials credentials = config.credentials();
81-
if (credentials != null) {
82+
String info = config.baseUri().getUserInfo();
83+
if (info != null && !info.trim().isEmpty()) {
84+
String[] parts = info.split(":", 2);
85+
String username = parts[0];
86+
String password = parts.length > 1 ? parts[1] : null;
87+
88+
Authenticator authenticator = new Authenticator() {
89+
@Override
90+
protected PasswordAuthentication getPasswordAuthentication() {
91+
return new PasswordAuthentication(username, password.toCharArray());
92+
}
93+
};
94+
builder = builder.authenticator(authenticator);
95+
} else if (credentials != null) {
8296
if (!(credentials instanceof UsernameAndPassword)) {
83-
throw new IllegalArgumentException("Credentials must be a user name and password: " + credentials);
97+
throw new IllegalArgumentException(
98+
"Credentials must be a user name and password: " + credentials);
8499
}
85100
UsernameAndPassword uap = (UsernameAndPassword) credentials;
86101
Authenticator authenticator = new Authenticator() {

0 commit comments

Comments
 (0)