我收到javax.net.ssl.SSLHandshakeException:当我尝试Https获取Web服务时握手异常期间远程主机关闭连接
Caused by: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:946)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:220)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:164)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:139)
at com.cl.fwk.services.http.impl.WebServiceImpl.get(WebServiceImpl.java:241)
... 28 more
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(InputRecord.java:482)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
... 46 more
我的HTTP连接器配置:
Connector connector = new Connector(Http11NioProtocol.class.getName());
Http11NioProtocol protocol = (Http11NioProtocol)
connector.getProtocolHandler();
protocol.setKeystoreFile("./Tests/Properties/keystore_local.ks");
protocol.setKeystorePass("changeit");
protocol.setSSLEnabled(true);
protocol.setSslProtocol("TLS"); // TLSv1
protocol.setClientAuth("true");
protocol.setMaxThreads(200);
protocol.setSecure(true);
connector.setPort(443);
protocol.setConnectionTimeout(30000);
tomcat.getService().addConnector(connector);
无论如何,我可以对我的代码做些什么来解决这个问题?这是完整的错误:
main, WRITE: TLSv1 Handshake, length = 48
main, received EOFException: error
main, handling exception: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
%% Invalidated: [Session-2, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA]
main, SEND TLSv1 ALERT: fatal, description = handshake_failure
main, WRITE: TLSv1 Alert, length = 32
main, called closeSocket()
答案 0 :(得分:1)
这是安全协议的问题。您正在使用TLSv1,但主机只接受TLSv1.1和TLSv1.2,那么您必须使用以下说明更改Java中的协议:
protocol.setProperty(“https.protocols”,“TLSv1.1”);
答案 1 :(得分:0)
尝试将protocol.setSslProtocol("TLS")
更改为“TLSv1.1”或“TLSv1.2”
答案 2 :(得分:0)
您可以做两件事: 您尝试访问的URL可能没有CA认证证书,因此要求提供证书,或者您可以在cacerts(java运行时)中导入证书,然后尝试访问或者其他:您可以禁用信任库(信任所有证书),这显然不在所有从安全角度推荐。
您可以参考here