我尝试连接到部署在Windows Server上的应用程序。 我使用NTLM。我正在使用Ubuntu和Java8。
我正在获取HTTP 401
代码,但前提是我尝试连接
来自我的Java应用程序。卷曲请求完全相同
身份验证详细信息正常,我收到HTTP 200
:
curl -v -L --ntlm -u 'myuser\mydomain:mypass' 'http://myip/api/element/151
这是我的Java代码 (我简化了一下,只包括与问题相关的东西):
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
...
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
...
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.RequestEntityProcessing;
...
class MyConnector {
private final Client client;
...
protected ClientConfig prepareClientConfig() {
ClientConfig config = new ClientConfig();
config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
config.property(ClientProperties.FOLLOW_REDIRECTS, true);
config.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new NTCredentials("myuser", "mypassword",
"mydomain", "mydomain"));
config.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
config.connectorProvider(new ApacheConnectorProvider());
return config;
}
...
MyConnector(...) {
...
client = ClientBuilder.newClient(prepareClientConfig());
}
protected String getDocString(...) throws MyException {
WebTarget target = client.target("http://myipi/api").path("element/151");
Invocation.Builder temp = target.request();
Response response = target.request().get();
if (response.getStatus() == Status.OK.getStatusCode()) {
String docString = response.readEntity(String.class);
return docString;
}
throw new MyException("Couldn't obtain doc. HTTP error code: " + response.getStatus());
}
...
}
调用getDocString
函数需要5-15(!)分钟。
我也相信应用程序会占用异常大量的RAM,
虽然没有其他功能同时运行(程序未并行化)。
然后我得到HTTP 401
代码。
我的Java应用程序工作时有一些情况。故事是:
401
,我的卷曲请求得到401
Ubuntu 14 LTS
升级到Ubuntu 16.0.2 LTS
,我的应用获得200
,卷曲得到200
Ubuntu 16.0.2 LTS
,我的应用获得了401
,curl得到了200
Ubuntu 17.04
(非LTS),我的应用获得200
一次,但我不能
重现它,现在完全相同的请求获得401
,curl得到200
我知道这个问题:
How to send NTLM authenticated post request using jersey?
我使用了代码,它允许我的应用程序与服务器升级(点3.
)结合使用。
但我不知道如何让它再次发挥作用。
我使用mitmproxy来调试请求,输出看起来像这样
(我模糊了Authorization
/ WWW-Authenticate
字段):
Proxy server listening at http://0.0.0.0:8080
127.0.0.1:39622: clientconnect
127.0.0.1:39622: request
-> Request(GET /api/element/151)
127.0.0.1:39622: serverconnect
-> myip:80
127.0.0.1:39622: response
-> Response(401 Unauthorized, text/html, 1.26k)
127.0.0.1:39622: GET http://myip/api/element/151
User-Agent: Jersey/2.26-b03 (Apache HttpClient 4.5.3)
Host: myip
Connection: Keep-Alive
Accept-Encoding: gzip,deflate
<< 401 Unauthorized 1.26k
Content-Type: text/html
Server: Microsoft-IIS/8.5
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
Date: Mon, 29 May 2017 13:38:36 GMT
Content-Length: 1293
127.0.0.1:39622: request
-> Request(GET /api/element/151)
127.0.0.1:39622: response
-> Response(401 Unauthorized, text/html; charset=us-ascii, 341b)
127.0.0.1:39622: GET http://myip/api/element/151
User-Agent: Jersey/2.26-b03 (Apache HttpClient 4.5.3)
Host: myip
Connection: Keep-Alive
Accept-Encoding: gzip,deflate
Authorization: NTLM TlRAAA==
<< 401 Unauthorized 341b
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
WWW-Authenticate: NTLM TlRAAABBBBBBAAA==
Date: Mon, 29 May 2017 13:38:36 GMT
Content-Length: 341
127.0.0.1:39622: request
-> Request(GET /api/element/151)
127.0.0.1:39622: server communication error: TcpDisconnect('[Errno 104] Connection reset by peer',)
127.0.0.1:39622: serverdisconnect
-> myip:80
127.0.0.1:39622: serverconnect
-> myip:80
127.0.0.1:39622: response
-> Response(401 Unauthorized, text/html, 1.26k)
127.0.0.1:39622: GET http://myip/api/element/151
User-Agent: Jersey/2.26-b03 (Apache HttpClient 4.5.3)
Host: myip
Connection: Keep-Alive
Accept-Encoding: gzip,deflate
Authorization: NTLM TlRAAABBBDDDAAA==
<< 401 Unauthorized 1.26k
Content-Type: text/html
Server: Microsoft-IIS/8.5
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
Date: Mon, 29 May 2017 13:44:35 GMT
Content-Length: 1293
127.0.0.1:39622: serverdisconnect
-> myip:80
127.0.0.1:39622: clientdisconnect
我将非常感谢有关此事的任何评论。
更新:我也试过Debian8和Debian9。结果是一样的。我得到了401
广告Debian8,更新到Debian9,得到200
一次,然后连续401
(对于完全相同的请求)。
答案 0 :(得分:0)
我通过使用JCIFS库而不是HttpClient的默认NTLM身份验证解决了这个问题。您可以在Apache页面上看到该示例: https://hc.apache.org/httpcomponents-client-4.5.x/ntlm.html
问题是我找不到让它与泽西兼容的方法。 Jersey使用javax.ws.rs.client.Client
,我不知道如何更新它的AuthScheme(没有设置它的方法)。所以我切换到org.apache.http.impl.client.CloseableHttpClient
并重写整个应用程序以使用原始的HttpClient方法。