Java 11 HttpClient 未发送基本身份验证
Java 11 HttpClient not sending basic authentication
我写了下面的 HttpClient 代码,它没有导致 Authorization
header 被发送到服务器:
public static void main(String[] args) {
var client = HttpClient.newBuilder()
.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password".toCharArray());
}
})
.version(HttpClient.Version.HTTP_1_1)
.build();
var request = HttpRequest.newBuilder()
.uri("https://service-that-needs-auth.example/")
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
}
我正在调用的服务收到 HTTP 401 错误。就我而言,它是 Atlassian Jira Cloud API。
我已经确认我的 getPasswordAuthentication()
方法没有被 HttpClient 调用。
为什么它不起作用,我应该怎么做?
我调用的服务(在本例中为 Atlassian 的 Jira Cloud API)同时支持基本和 OAuth 身份验证。我试图使用 HTTP Basic,但它发回了 OAuth 的身份验证质询。
从当前的 JDK 11 开始,HttpClient 不会发送基本凭据,直到服务器用 WWW-Authenticate header 对它们进行质询。此外,它理解的唯一挑战类型是基本身份验证。 The relevant JDK code is here(完成 TODO 以支持比基本身份验证更多的功能)如果您想看一看。
同时,我的补救措施是绕过 HttpClient 的身份验证 API 并自己创建和发送基本授权 header:
public static void main(String[] args) {
var client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.build();
var request = HttpRequest.newBuilder()
.uri(new URI("https://service-that-needs-auth.example/"))
.header("Authorization", basicAuth("username", "password"))
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
}
private static String basicAuth(String username, String password) {
return "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
}
我写了下面的 HttpClient 代码,它没有导致 Authorization
header 被发送到服务器:
public static void main(String[] args) {
var client = HttpClient.newBuilder()
.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password".toCharArray());
}
})
.version(HttpClient.Version.HTTP_1_1)
.build();
var request = HttpRequest.newBuilder()
.uri("https://service-that-needs-auth.example/")
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
}
我正在调用的服务收到 HTTP 401 错误。就我而言,它是 Atlassian Jira Cloud API。
我已经确认我的 getPasswordAuthentication()
方法没有被 HttpClient 调用。
为什么它不起作用,我应该怎么做?
我调用的服务(在本例中为 Atlassian 的 Jira Cloud API)同时支持基本和 OAuth 身份验证。我试图使用 HTTP Basic,但它发回了 OAuth 的身份验证质询。
从当前的 JDK 11 开始,HttpClient 不会发送基本凭据,直到服务器用 WWW-Authenticate header 对它们进行质询。此外,它理解的唯一挑战类型是基本身份验证。 The relevant JDK code is here(完成 TODO 以支持比基本身份验证更多的功能)如果您想看一看。
同时,我的补救措施是绕过 HttpClient 的身份验证 API 并自己创建和发送基本授权 header:
public static void main(String[] args) {
var client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.build();
var request = HttpRequest.newBuilder()
.uri(new URI("https://service-that-needs-auth.example/"))
.header("Authorization", basicAuth("username", "password"))
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
}
private static String basicAuth(String username, String password) {
return "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
}