我正在尝试连接到来自外部站点的响应
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(InputRecord.java:505)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
... 56 more
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
我的代码:
CloseableHttpClient httpClient2 = null;
try {
httpClient2 = (CloseableHttpClient) getTLS();
} catch (KeyManagementException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnrecoverableKeyException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (KeyStoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
public HttpClient getTLS() throws KeyManagementException,
UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException {
SSLContext sSLContext = SSLContext.getInstance("TLS");
sSLContext.init(null, null, null);
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sSLContext,
new String[] { "TLS" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
return httpClient;
}
我已将 .pem 证书添加到密钥库
myserverURL.pem /usr/local/openjdk-8/jre/lib/security
我尝试检查以下命令,发现正在使用 TLSv1.3。
curl --insecure -vvI https://myserverURL.com 2>&1 | awk 'BEGIN { cert=0 } /^\* SSL connection/ { cert=1 } /^\*/ { if (cert) print }'
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server did not agree to a protocol
客户有:
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
这之前工作得很好。我猜想服务器端的协议已更改为 TLSv1.3。 1.2可以连接到1.3吗
如果服务器带有
-tls1_3
标记,那么它将“仅使用 TLSv1.3”。服务器需要将其删除才能与客户端兼容。不过,服务器使用可能是一种安全架构设计TLSv1.2
请参阅 ssl.com 上的这篇文章:TLS 1.3 将会继续存在“TLS 1.3 放弃了向后兼容性,转而采用适当的安全设计。它从头开始设计,提供与 TLS 1.2 类似(但不兼容)的功能,但显着提高了性能、隐私和安全性。”各种风格的 OpenJDK 8 在不同的发布点向后移植了对 TLS 1.3 的支持,请参阅下面的一些示例:
发布 | |
---|---|
8u272 | |
8u262/8u272/8u292 | |
8u261 | |
8u272 | |
8u292 |
您可以通过运行以下命令来做到这一点:
TLSv1.3
这将打印类似
public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
String[] supportedProtocols = context.getDefaultSSLParameters().getProtocols();
System.out.println(Arrays.toString(supportedProtocols));
}
的内容如果您拥有的风味没有返回
[TLSv1.3, TLSv1.2]
,那么您需要检查他们的版本,看看他们是否向后移植了对
TLSv1.3
的支持。如果他们这样做了,请更新到该版本,如果没有,您可能必须使用另一种风格的 OpenJDK 8 或更好的升级到 OpenJDK 11(取决于具体情况)
TLSv1.3
将这些证书导入 JDK 安全 cacerts 文件后,错误消失了。