SSL请求仅在生产中失败

时间:2016-12-19 10:09:43

标签: java ssl httpclient

我在运行Java 7(Oracle JVM)的Tomcat 7上部署了一个Java Web应用程序。

我对外部HTTPS网址的所有Apache HTTPClient请求都失败了(仅在生产中),我不知道为什么。我检查了随JVM一起安装的cacerts信任库,并且所有证书都有效。

我已经将JVM和Tomcat目录下载到我的开发机器并运行了webapp(具有相同的路径,相同的参数等)并且它可以工作。

执行在生产中发出HTTPS请求的独立JAR也可以。

此时,我坦率地说出了自己的想法。

这是失败的示例代码:

  InputStream is=null;

  HttpParams httpParams = new BasicHttpParams();
  HttpConnectionParams.setConnectionTimeout(httpParams, 2000);
  HttpConnectionParams.setSoTimeout(httpParams, 2000);

  DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
  try {
      httpclient.getParams().setParameter("http.useragent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
      HttpContext localContext = new BasicHttpContext();

      HttpHead httphead = new HttpHead(url); 
      HttpResponse response = httpclient.execute(httphead, localContext);

      HttpGet httpget = new HttpGet(url); 
      response = httpclient.execute(httpget, localContext);

      HttpEntity entity = response.getEntity();
      is= new ByteArrayInputStream(EntityUtils.toByteArray(entity));

  } catch (IOException ie) {
      throw new IOException("IO exception",ie);
  } finally {
      httpclient.getConnectionManager().shutdown();
  }
  return is;

stacktrace示例:

Caused by: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
        at sun.security.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:421)
        at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
        at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:390)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
        at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
        at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
        at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:561)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)

Tomcat JAVA_OPTS:

JAVA_OPTS="-server -Xms4g -Xmx4g -XX:PermSize=512m -XX:MaxPermSize=512m -XX:+UseConcMarkSweepGC -XX:+DisableExplicitGC -Dfile.encoding=UTF-8 -Djavax.net.ssl.trustStore=/usr/lib/jvm/java-7-oracle/jre/lib/security/cacerts -Djavax.net.ssl.trustStorePassword=changeit"

3 个答案:

答案 0 :(得分:3)

很难说无法访问您的环境。我要检查的第一件事:

  • 确保您确实使用了您认为正在使用的JVM和信任存储
  • 检查用户主目录中的.ssh目录。
  • 你有本地运行的任何ssl工具,比如plink?
  • 您是否设置了与SSL相关的环境变量?
  • 生产服务器上有哪些防火墙设置?

答案 1 :(得分:0)

您需要使用端口8443配置https 请参阅以下链接了解更多详情 https://www.mkyong.com/tomcat/how-to-configure-tomcat-to-support-ssl-or-https/

答案 2 :(得分:0)

事实证明,与Oracle Java捆绑在一起的cacerts文件中的证书不包含我向其发送请求的网站的根证书。我用OpenJDK包中的cacerts文件替换了该文件,一切都恢复了。