【问题标题】:HttpClient 4.2.3 using both SSL encryption and NTLM authentication fails同时使用 SSL 加密和 NTLM 身份验证的 HttpClient 4.2.3 失败
【发布时间】:2013-02-14 22:07:57
【问题描述】:

我正在尝试通过来自 Liferay 6.1 portlet 的 HTTPClient 4.2.3 对 Sharepoint 2010 进行 REST 调用。

我已将证书导入本地 MAC 的 JVM cacerts,并尝试将 cacerts 作为密钥库加载。

我的代码是:

String opsCalendarURL1 = "https://hostname/sites/team-sites/operations/_vti_bin/owssvr.dll?";
String opsCalendarURL2 = "Cmd=Display&List={6E460908-D470-4F8A-AF76-CC279E25E0B1}&XMLDATA=TRUE";
String opsCalenderURLEncoded = opsCalendarURL1 + URLEncoder.encode( opsCalendarURL2 , "UTF8" );

System.out.println(opsCalenderURLEncoded);
DefaultHttpClient httpclient = new DefaultHttpClient();

try {
   // SSL
   KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());
   FileInputStream instream = new FileInputStream(new File("/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/security/cacerts"));
   try {
    trustStore.load(instream, "changeit".toCharArray());
   } finally {
       try { instream.close(); } catch (Exception ignore) {}
   }

   SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
   Scheme sch = new Scheme("https", 443, socketFactory);
   httpclient.getConnectionManager().getSchemeRegistry().register(sch);
   System.out.println("----------------------------------------");

   HttpHost targetHost = new HttpHost("hostname", 443, "https");
   httpclient.getCredentialsProvider().setCredentials(
    AuthScope.ANY,
    new NTCredentials("username", "password","machine","domain"));

   HttpGet httpget = new HttpGet(opsCalenderURLEncoded);

   System.out.println("executing request: " + httpget.getRequestLine());
   System.out.println("to target: " + targetHost);

   HttpResponse response2 = httpclient.execute(targetHost, httpget);
   HttpEntity entity = response2.getEntity();

   System.out.println("----------------------------------------");
   System.out.println(response2.getStatusLine());
   System.out.println(response2.getProtocolVersion());
   if (entity != null) {
     System.out.println("Response content length: " + entity.getContentLength());
   }
   EntityUtils.consume(entity);
  } finally {
    httpclient.getConnectionManager().shutdown();
}

我总是得到的回应是: HTTP/1.1 401 未经授权

我在线路日志中没有看到 SSL 握手,并且收到了 401 未经授权的响应。我已经尝试了具有相同结果的示例代码的各种组合。

注意 - 我已经使用 FireFox 和 CURL 来做同样的事情,我在这里以编程方式尝试做同样的事情,而且效果很好。所以服务器似乎设置正确。 CURL 详细日志显示 SSL 握手首先发生,NTLM 成功作为下一步。

如果需要,我可以附上电线日志。

非常感谢您的宝贵时间!

感谢任何帮助和指点。

【问题讨论】:

  • 你试过用VM参数运行客户端吗... -Djavax.net.debug=all
  • @helios - 感谢您的建议!是的,我刚刚尝试过,我看到加密正在发生。查看电报日志,我看到以下 '2013/02/15 13:46:48:224 GMT [DEBUG] DefaultHttpClient - 授权质询已处理 2013/02/15 13:46:48:224 GMT [DEBUG] DefaultHttpClient -身份验证失败'
  • > 尝试更新 HttpClient[在此处输入链接描述][1] [1]:stackoverflow.com/questions/5917356/…

标签: ssl sharepoint-2010 httpclient liferay-6 ntlm


【解决方案1】:

问题似乎在于使用 NTLM v2 和 Windows 2008 R2 的 IIS 7.5 和 HTTPClient。

我切换到 Java HTTPURLConnection,它工作得很好。

Post with some detail on another issue with the same code here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    相关资源
    最近更新 更多