我在运行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"
答案 0 :(得分:3)
很难说无法访问您的环境。我要检查的第一件事:
答案 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文件替换了该文件,一切都恢复了。