gpt4 book ai didi

Java 1.6 TLS 1.2 握手失败

转载 作者:搜寻专家 更新时间:2023-11-01 03:48:43 53 4
gpt4 key购买 nike

我正在尝试使用 java jdk 1.6.30 与使用 TLS 1.2 的 URI 通信,我尝试在我的系统上设置 BouncyCaSTLe 提供程序,因为默认情况下 java jdk 1.6.30 不支持 TLS 1.2 我还安装了我的本地计算机上的证书,但我收到以下错误:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1806)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:986)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1170)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1197)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1181)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:434)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1014)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)
at main.main(main.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

我使用的代码如下:

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.net.*;
import java.security.Security;

public class main {

static {
Security.addProvider(new BouncyCastleProvider());
}

public static void main(String[] args) throws Exception {
final URL url = new URL("URL");
final String postData = "POST_DATA";
final byte[] postDataBytes = postData.getBytes("UTF-8");
final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
httpURLConnection.setDoOutput(true);
httpURLConnection.getOutputStream().write(postDataBytes);
httpURLConnection.getInputStream();
System.out.println("OK");
}
}

我尝试用谷歌搜索,但没有找到任何解决方案( TLS 1.2 + Java 1.6 + BouncyCastleReceived fatal alert: handshake_failure with Tomcat 等)。

有什么我可以尝试信任证书的吗?

提前致谢

最佳答案

你可以试试这段代码,我用的是BouncyCaSTLe 1.60和Java 1.6_65

BouncyCastleJsseProvider provider = new BouncyCastleJsseProvider(new BouncyCastleProvider());
SSLContext context = SSLContext.getInstance("TLS", provider);
TrustAllX509TrustManager manager = new TrustAllX509TrustManager();

context.init(null, new X509TrustManager[] { manager }, new SecureRandom());

URL url = new URL("https://stackoverflow.com:443");

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setSSLSocketFactory(context.getSocketFactory());

connection.connect();

实际上,java 1.6 的最新版本之一支持 TLS 1.2 https://www.oracle.com/technetwork/java/javase/overview-156328.html#R160_121

关于Java 1.6 TLS 1.2 握手失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34789216/

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