![](/img/trans.png)
[英]Apache HttpClient v5.0 doesn't authenticate with NTLM on Windows
[英]java - HttpClient doesn't authenticate with NTLM
嘗試連接到受NTLM身份驗證保護的資源。 發出請求時,我得到未經身份驗證的響應401,但此后httpclient不會執行NTLM身份驗證。
添加了Interceptor來查看通信,它甚至不嘗試進行身份驗證:
Request:
POST/NAV/xxxxxxxxx
Content-type: text/xml; charset=utf-8
SOAPAction:
Content-Length: 359
Host: xxx.local:7051
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.4 (Java/1.8.0_181)
Accept-Encoding: gzip,deflate
Response:
Unauthorized
Content-Length: 0
Server: Microsoft-HTTPAPI/2.0
WWW-Authenticate: Negotiate
Date: Wed, 26 Sep 2018 10:37:56 GMT
此后沒有任何請求。
有什么建議在這里有什么問題嗎?
這是我的代碼:
NTCredentials credentials = new NTCredentials("testuser", "pass1", null, "stt.local");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, credentials);
ArrayList<String> authPrefs = new ArrayList<String>();
authPrefs.add(AuthSchemes.NTLM);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(30000)
.setConnectTimeout(30000)
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM))
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
.build();
HttpClient client = HttpClientBuilder.
create().
setDefaultCredentialsProvider(credsProvider).
setDefaultRequestConfig(requestConfig).
addInterceptorLast(new LoggingRequestInterceptor()).
addInterceptorLast(new LoggingResponseInterceptor()).
build();
HttpPost post = new HttpPost(endpoint); //Provide Request URL
try {
StringEntity input = new StringEntity(bodyAsString);
input.setContentType("text/xml; charset=utf-8");
post.setEntity(input);
post.setHeader("Content-type", "text/xml; charset=utf-8");
post.setHeader("SOAPAction", ""); //Provide Soap action
org.apache.http.HttpResponse response = client.execute(post);
}
該NTCredentials
構造函數的參數應具有單獨的用戶名和域名。
參數:
userName-用戶名。 這不應包括要進行身份驗證的域。 例如:“用戶”是正確的,而“域\\用戶”是不正確的。
密碼-密碼。
工作站-身份驗證請求所源自的工作站。 本質上,這台機器的計算機名稱。
域-要在其中進行身份驗證的域。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.