我正在尝试在使用 SSL 加密 (https) 以及 NTLM 身份验证的 Sharepoint 2010 服务器上执行简单的 REST 调用。当服务器设置为不需要 SSL 时(仅用于测试,服务器将在生产中需要 SSL),我的 NTLM 身份验证和后续 REST 调用使用 HttpClient 工作正常。但是,启用 SSL 后,身份验证将不再有效。
这是 SSL 处理的代码(设置为接受所有证书):
SSLContext sslContext = SSLContext.getInstance("TLS");
// set up a TrustManager that trusts everything
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs,
String authType) { }
public void checkServerTrusted(X509Certificate[] certs,
String authType) { }
} }, new SecureRandom());
SSLSocketFactory sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme httpsScheme = new Scheme("https", 443, sf);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(httpsScheme);
ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);
后面是执行 NTML 和 HTTP GET 调用的代码:
HttpParams params = new BasicHttpParams();
DefaultHttpClient httpclient = new DefaultHttpClient(cm, params);
// Set up the NTLM credentials
NTCredentials creds = new NTCredentials(USERNAME, PASSWORD, MACHINE, DOMAIN);
httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
// Setup the HTTP GET and execute it
HttpHost target = new HttpHost(HOSTNAME, 443, "https");
HttpContext localContext = new BasicHttpContext();
HttpGet httpget = new HttpGet("/site/_vti_bin/ListData.svc/TestList");
HttpResponse response1 = httpclient.execute(target, httpget, localContext);
我总是得到的回应是:
HTTP/1.1 400 Bad Request
The server encountered an error processing the request. See server logs for more details.
请注意,我已经使用 FireFox Poster 和 CURL 以编程方式完成了我在此处尝试执行的相同操作,并且工作正常。所以服务器似乎设置正确。
感谢您的宝贵时间!
最佳答案
为可能遇到此问题的其他人记录此问题。
问题是 IIS 服务器设置为允许匿名登录。显然,存在一个错误,如果设置了此错误,则该安装下与共享点实例的任何 REST 或 SOAP 连接都不会成功。似乎字符串“anonymous”被添加到 REST/SOAP URL 中,这当然使它指向一个不存在的服务。在我们关闭允许在 IIS 实例上进行匿名登录后,调用就起作用了。
关于java - 使用 SSL 加密和 NTLM 身份验证的 HttpClient 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9714615/