摘要式身份验证java.net.HttpClient



我正在尝试连接到一个受摘要身份验证保护的网站。如果我试图通过Insomnia或Firefox登录,但无法在Java 17中使用,我的凭据可以正常工作(Insomnias自动生成的代码也不起作用(。

我试图遵循并理解以下教程/文档:

https://www.baeldung.com/java-9-http-client

https://docs.oracle.com/javase/7/docs/technotes/guides/net/http-auth.html

据我所知,两人都提到《文摘》是受支持的。

我得到的结果总是状态代码401&摘要身份验证失败时的预期标头:

www authenticate=〔摘要领域="api领域",qop="auth",nonce="nonce==">

这是当前的代码。方法getPasswordAuthentication未执行:

public void checkIsAPIRunning() {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://the-site-I-try-to-connect-with:443/api/function"))
.method("GET", HttpRequest.BodyPublishers.noBody()).build();
HttpResponse<String> response = null;
try {
response = HttpClient.newBuilder().authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "pass".toCharArray());
}
}).build().send(request, BodyHandlers.ofString());          
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

我是不是误解了医生?如果有任何帮助或建议,我将不胜感激:(

新的HttpClient不直接支持摘要式身份验证-请参阅此处:https://bugs.openjdk.org/browse/JDK-8285888

您似乎需要自己处理身份验证过程。

您必须实现握手算法,我建议首先使用curl来验证流程。

我按照这个算法实现:

  1. 调用摘要端点:响应为401,标头为www-authenticate
  2. 从标头www-authenticate中获取参数realmqopnonce
  3. 构建HA1HA2response和摘要标头Authorization
  4. 第二次使用标头Authorization调用端点
  5. 响应应该是200 OK

使用Spring验证的摘要式实现:https://github.com/ron190/jsql-injection/blob/master/model/src/main/java/com/jsql/util/DigestUtil.java

受以下资源启发:

  • https://en.wikipedia.org/wiki/Digest_access_authentication
  • https://gist.github.com/usamadar/2912088

最新更新