【问题标题】:Http post requests unsing NTLM Authentication (java)Http post 请求取消 NTLM 身份验证(java)
【发布时间】:2017-02-20 16:47:55
【问题描述】:

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

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

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

【问题讨论】:

    标签: rest http http-post windows-authentication ntlm


    【解决方案1】:

    我仍然不知道为什么来自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);
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-23
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      相关资源
      最近更新 更多