gpt4 book ai didi

java - EC2 机器中的 SSL 握手异常

转载 作者:太空宇宙 更新时间:2023-11-03 14:43:45 24 4
gpt4 key购买 nike

我想使用普通 HTTP 请求连接到站点:

import javax.net.ssl.*;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class SSLTest {
public static void main(String [] args) throws Exception {
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom());
SSLContext.setDefault(ctx);
URL url = new URL("https://www.1aauto.com/1961-64-chevy-bel-air-biscayne-impala-fuel-tank-with-strap-set/i/1afrk00087");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
System.out.println(conn.getResponseCode());
conn.disconnect();
}
private static class DefaultTrustManager implements X509TrustManager {

@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}

@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}

@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
}

现在,在我的本地,它工作正常并给出 200 个响应。但是在 AWS EC2 实例上,抛出异常

Exception in thread "main" javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:953)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1301)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
at SSLTest.main(SSLTest.java:24)
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:934)
... 9 more

这里缺少什么。请帮忙。

最佳答案

您的本地和 EC2 Java 版本是否相同? www.1aauto.com only supports TLS 1.2它在 Java 1.8 中默认启用,但在早期版本中不启用。

Protocols section of SSL Labs report showing only TLS 1.2 enabled Handshake simulation section of SSL Labs report showing errors with older version of Java with default settings

此外,www.1aauto.com 提供了默认 Java 信任库信任的有效证书。您究竟为什么要使用绝育版本的 TrustManager 和 HostnameVerifier?不建议在生产环境中使用此类不安全的代码。

关于java - EC2 机器中的 SSL 握手异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41050002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com