我尝试使用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 + BouncyCastle,Received fatal alert: handshake_failure with Tomcat等)。
我可以尝试信任证书吗?
提前致谢
答案 0 :(得分:0)
您可以尝试这段代码,我使用的是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