Http post 请求取消 NTLM 身份验证(java)

use*_*169 2 rest ntlm http http-post windows-authentication

我尝试在 Java 中使用 NTLM 身份验证发送 HttpRest 调用。我尝试使用org.apache.http图书馆。使用 HttpClient 发送带有匿名身份验证的 Post 请求并不是什么大问题。但是我在使用 WindowsAuthentication 时遇到了一些麻烦。我什至尝试将 CredentialProvider 与我自己的 Windows 凭据一起使用(作为妥协,我也不喜欢那样)但我没有成功。

是否有使用 NTLM 身份验证从 Java 代码发送 post 请求的简单方法?

是否有另一个库更适合我的需求?

use*_*169 6

我仍然不知道为什么https://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html 中关于 NTLM 身份验证的 doku对我不起作用。我终于解决了我的问题,类似于http://www.baeldung.com/httpclient-post-http-request 上描述的基本身份验证文档

现在看起来像这样:

...
CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY,
                new NTCredentials("username", "passwd", hostname, "domain.at"));


HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();

HttpPost post = new HttpPost("http://www.example.com"));

StringEntity input = new StringEntity(bodyAsString, HTTP.UTF_8);
input.setContentType("application/json");
input.setContentEncoding("UTF-8");
post.setEntity(input);

post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");


HttpResponse response = client.execute(post);
...
Run Code Online (Sandbox Code Playgroud)