我正在尝试在使用SSL加密(https)的Sharepoint 2010服务器上执行简单的REST调用,以及NTLM身份验证。当服务器设置为不需要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来执行我在这里尝试以编程方式执行的操作,并且它工作正常。所以服务器似乎设置正确。
谢谢你的时间!
答案 0 :(得分:4)
将此文档记录给可能遇到此问题的其他人。
问题是IIS服务器设置为允许匿名登录。显然,如果设置了此错误,那么在该安装下与sharepoint实例的任何REST或SOAP连接都将无法成功。似乎字符串“anonymous”被添加到REST / SOAP URL中,这当然使其指向不存在的服务。在我们关闭允许匿名登录IIS实例后,调用工作。
答案 1 :(得分:0)
我的猜测是由于SSL连接没有持久性,NTLM身份验证失败。 NTLM方案需要多个消息交换,并且只能使用持久连接。
如果您发布HTTP会话的连线/上下文日志(此处或httpclient-users列表),我应该能够说明为什么SSL连接不会停留。