Apache HttpClient v5.0在Windows上无法使用NTLM进行身份验证。

huangapple go评论104阅读模式
英文:

Apache HttpClient v5.0 doesn't authenticate with NTLM on Windows

问题

  1. 我正在尝试通过 NTLM 代理发出请求代码如下
  2. try (CloseableHttpClient httpClient = WinHttpClients.createDefault()) {
  3. HttpHost target = HttpHost.create("http://example.com");
  4. HttpHost proxy = new HttpHost("http", "localhost", 80);
  5. RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
  6. HttpGet request = new HttpGet("/");
  7. request.setConfig(config);
  8. try (CloseableHttpResponse response = httpClient.execute(target, request)) {
  9. StatusLine statusLine = new StatusLine(response);
  10. if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
  11. System.out.println("Test OK");
  12. } else if (statusLine.getStatusCode() == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
  13. throw new CredentialException(statusLine.getReasonPhrase());
  14. }
  15. }
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }

错误日志如下:

[主要] DEBUG org.apache.hc.client5.http.impl.classic.InternalHttpClient - ex-00000001: 准备请求执行
...
...
javax.security.auth.login.CredentialException: 需要代理授权
at org.kpax.http_client_5_testing.App.main(App.java:47)

  1. 环境:
  2. - 操作系统:Windows 10
  3. - JREAdoptOpenJdk v11
  4. - 代理类型:NTLMWingate
  5. 问题:上述代码是否存在问题?
  6. 注:相同的代码在 Apache Http Client v4.5.12 中正常工作。
  7. <details>
  8. <summary>英文:</summary>
  9. I&#39;m trying to make a request through a NTLM proxy like this:
  10. try (CloseableHttpClient httpClient = WinHttpClients.createDefault()) {
  11. HttpHost target = HttpHost.create(&quot;http://example.com&quot;);
  12. HttpHost proxy = new HttpHost(&quot;http&quot;, &quot;localhost&quot;, 80);
  13. RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
  14. HttpGet request = new HttpGet(&quot;/&quot;);
  15. request.setConfig(config);
  16. try (CloseableHttpResponse response = httpClient.execute(target, request)) {
  17. StatusLine statusLine = new StatusLine(response);
  18. if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
  19. System.out.println(&quot;Test OK&quot;);
  20. } else if (statusLine.getStatusCode() == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
  21. throw new CredentialException(statusLine.getReasonPhrase());
  22. }
  23. }
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. but it fails, here is the log:
  28. [main] DEBUG org.apache.hc.client5.http.impl.classic.InternalHttpClient - ex-00000001: preparing request execution
  29. [main] DEBUG org.apache.hc.client5.http.protocol.RequestAddCookies - Cookie spec selected: strict
  30. [main] DEBUG org.apache.hc.client5.http.protocol.RequestAuthCache - Auth cache not set in the context
  31. [main] DEBUG org.apache.hc.client5.http.impl.classic.ProtocolExec - ex-00000001: target auth state: UNCHALLENGED
  32. [main] DEBUG org.apache.hc.client5.http.impl.classic.ProtocolExec - ex-00000001: proxy auth state: UNCHALLENGED
  33. [main] DEBUG org.apache.hc.client5.http.impl.classic.ConnectExec - ex-00000001: acquiring connection with route {}-&gt;http://localhost:80-&gt;http://example.com:80
  34. [main] DEBUG org.apache.hc.client5.http.impl.classic.InternalHttpClient - ex-00000001: acquiring endpoint (3 MINUTES)
  35. [main] DEBUG org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager - ex-00000001: endpoint lease request (3 MINUTES) [route: {}-&gt;http://localhost:80-&gt;http://example.com:80][total available: 0; route allocated: 0 of 5; total allocated: 0 of 25]
  36. [main] DEBUG org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager - ex-00000001: endpoint leased [route: {}-&gt;http://localhost:80-&gt;http://example.com:80][total available: 0; route allocated: 1 of 5; total allocated: 1 of 25]
  37. [main] DEBUG org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager - ex-00000001: acquired ep-00000000
  38. [main] DEBUG org.apache.hc.client5.http.impl.classic.InternalHttpClient - ex-00000001: acquired endpoint ep-00000000
  39. [main] DEBUG org.apache.hc.client5.http.impl.classic.ConnectExec - ex-00000001: opening connection {}-&gt;http://localhost:80-&gt;http://example.com:80
  40. [main] DEBUG org.apache.hc.client5.http.impl.classic.InternalHttpClient - ep-00000000: connecting endpoint (3 MINUTES)
  41. [main] DEBUG org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager - ep-00000000: connecting endpoint to http://localhost:80 (3 MINUTES)
  42. [main] DEBUG org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator - http-outgoing-0: connecting to localhost/127.0.0.1:80
  43. [main] DEBUG org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator - http-outgoing-0: connection established 127.0.0.1:57194&lt;-&gt;127.0.0.1:80
  44. [main] DEBUG org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager - ep-00000000: connected http-outgoing-0
  45. [main] DEBUG org.apache.hc.client5.http.impl.classic.InternalHttpClient - ep-00000000: endpoint connected
  46. [main] DEBUG org.apache.hc.client5.http.impl.classic.MainClientExec - ex-00000001: executing GET http://example.com/ HTTP/1.1
  47. [main] DEBUG org.apache.hc.client5.http.impl.classic.InternalHttpClient - ep-00000000: start execution ex-00000001
  48. [main] DEBUG org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager - ep-00000000: executing exchange ex-00000001 over http-outgoing-0
  49. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &gt;&gt; GET http://example.com/ HTTP/1.1
  50. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &gt;&gt; Accept-Encoding: gzip, x-gzip, deflate
  51. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &gt;&gt; Host: example.com
  52. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &gt;&gt; User-Agent: Apache-HttpClient/5.0 (Java/1.8.0_222)
  53. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &gt;&gt; &quot;GET http://example.com/ HTTP/1.1[\r][\n]&quot;
  54. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &gt;&gt; &quot;Accept-Encoding: gzip, x-gzip, deflate[\r][\n]&quot;
  55. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &gt;&gt; &quot;Host: example.com[\r][\n]&quot;
  56. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &gt;&gt; &quot;User-Agent: Apache-HttpClient/5.0 (Java/1.8.0_222)[\r][\n]&quot;
  57. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &gt;&gt; &quot;[\r][\n]&quot;
  58. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;HTTP/1.1 407 Proxy authorization required[\r][\n]&quot;
  59. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;Proxy-Authenticate: NTLM[\r][\n]&quot;
  60. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;Proxy-Authenticate: Basic realm=&quot;/&quot;, charset=&quot;UTF-8&quot;[\r][\n]&quot;
  61. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;Proxy-Support: session-based-authentication[\r][\n]&quot;
  62. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;Cache-control: no-store, no-cache[\r][\n]&quot;
  63. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;Content-Type: text/html; charset=utf-8[\r][\n]&quot;
  64. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;Content-Length: 855[\r][\n]&quot;
  65. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;Date: Sun, 05 Apr 2020 06:15:20 GMT[\r][\n]&quot;
  66. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;Server: WinGate Engine[\r][\n]&quot;
  67. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;Connection: Keep-Alive[\r][\n]&quot;
  68. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;[\r][\n]&quot;
  69. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;&lt;html&gt;&lt;head&gt;&lt;title&gt;Proxy authorization required&lt;/title&gt;[\r][\n]&quot;
  70. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;&lt;style&gt;[\r][\n]&quot;
  71. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;body { font-family:helvetica; font-size:11pt; color:#0071BB; margin:30px; padding:0px; }[\r][\n]&quot;
  72. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;h1 { color:#005395; margin:0px 0px 0px 0px; }[\r][\n]&quot;
  73. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;h2 { color:#005395; }[\r][\n]&quot;
  74. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;p { margin:6px 0px 6px 0px;}[\r][\n]&quot;
  75. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;p.footer { font-size:8pt; margin:3px 0px 0px 0px;}[\r][\n]&quot;
  76. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;b { font-weight:550; color:#7F7F7F; }[\r][\n]&quot;
  77. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;&lt;/style&gt;[\r][\n]&quot;
  78. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;&lt;/head&gt;[\r][\n]&quot;
  79. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;&lt;body&gt;&lt;h1&gt;Proxy authorization required&lt;/h1&gt;[\r][\n]&quot;
  80. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;&lt;div style=&quot;margin:12px 0px 12px 0px; padding:12px 0px 12px 0px; min-height:300px; border-top:1px solid #0071BB; border-bottom:1px solid #0071BB;&quot;&gt;You must authenticate to gain access to this resource&lt;/div&gt;[\r][\n]&quot;
  81. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;&lt;p class=&quot;footer&quot;&gt;&lt;a href=&quot;http://www.wingate.com&quot;&gt;WinGate&lt;/a&gt;&amp;trade; &amp;copy; Qbik New Zealand Limited 1995-2020[\r][\n]&quot;
  82. [main] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;[\r][\n]&quot;
  83. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &lt;&lt; HTTP/1.1 407 Proxy authorization required
  84. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &lt;&lt; Proxy-Authenticate: NTLM
  85. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &lt;&lt; Proxy-Authenticate: Basic realm=&quot;/&quot;, charset=&quot;UTF-8&quot;
  86. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &lt;&lt; Proxy-Support: session-based-authentication
  87. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &lt;&lt; Cache-control: no-store, no-cache
  88. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &lt;&lt; Content-Type: text/html; charset=utf-8
  89. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &lt;&lt; Content-Length: 855
  90. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &lt;&lt; Date: Sun, 05 Apr 2020 06:15:20 GMT
  91. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &lt;&lt; Server: WinGate Engine
  92. [main] DEBUG org.apache.hc.client5.http.headers - http-outgoing-0 &lt;&lt; Connection: Keep-Alive
  93. [main] DEBUG org.apache.hc.client5.http.impl.classic.MainClientExec - ex-00000001: connection can be kept alive for 3 MINUTES
  94. [main] DEBUG org.apache.hc.client5.http.impl.classic.ProtocolExec - Authentication required
  95. [main] DEBUG org.apache.hc.client5.http.impl.classic.ProtocolExec - localhost:80 requested authentication
  96. [main] DEBUG org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy - Authentication schemes in the order of preference: [Negotiate, Kerberos, NTLM, Digest, Basic]
  97. [main] DEBUG org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy - Challenge for Negotiate authentication scheme not available
  98. [main] DEBUG org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy - Challenge for Kerberos authentication scheme not available
  99. [main] DEBUG org.apache.hc.client5.http.impl.win.WindowsNegotiateScheme - Created WindowsNegotiateScheme using NTLM
  100. [main] DEBUG org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy - Challenge for Digest authentication scheme not available
  101. [main] WARN org.apache.hc.client5.http.impl.classic.ProtocolExec - Missing auth challenge
  102. [main] DEBUG org.apache.hc.client5.http.impl.io.DefaultManagedHttpClientConnection - http-outgoing-0: close connection GRACEFUL
  103. [main] DEBUG org.apache.hc.client5.http.impl.classic.InternalHttpClient - ep-00000000: endpoint closed
  104. [main] DEBUG org.apache.hc.client5.http.impl.classic.InternalHttpClient - ep-00000000: endpoint closed
  105. [main] DEBUG org.apache.hc.client5.http.impl.classic.InternalHttpClient - ep-00000000: discarding endpoint
  106. [main] DEBUG org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager - ep-00000000: releasing endpoint
  107. [main] DEBUG org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager - ep-00000000: connection released [route: {}-&gt;http://localhost:80-&gt;http://example.com:80][total available: 0; route allocated: 0 of 5; total allocated: 0 of 25]
  108. [main] DEBUG org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager - Shutdown connection pool GRACEFUL
  109. [main] DEBUG org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager - Connection pool shut down
  110. javax.security.auth.login.CredentialException: Proxy authorization required
  111. at org.kpax.http_client_5_testing.App.main(App.java:47)
  112. ----------
  113. **Environment**
  114. - OS: Windows 10
  115. - JRE: AdoptOpenJdk v11
  116. - Proxy type: NTLM (Wingate)
  117. ----------
  118. **Question** Is there something wrong with the above code?
  119. ----------
  120. **Obs** The same code works fine with Apache Http Client v4.5.12
  121. </details>
  122. # 答案1
  123. **得分**: 1
  124. 这是一个[轻微的错误][1],已在v5.0.1中解决。
  125. [1]: https://issues.apache.org/jira/browse/HTTPCLIENT-2073
  126. <details>
  127. <summary>英文:</summary>
  128. It is a [minor bug][1], solved now, for v5.0.1.
  129. [1]: https://issues.apache.org/jira/browse/HTTPCLIENT-2073
  130. </details>

huangapple
  • 本文由 发表于 2020年4月5日 16:13:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61039771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定