Ich habe in einem Artikel gelesen, dass HttpsURLConnection
die SSL-Verbindung transparent aushandelt.
Das offizielle Dokument besagt:
Diese Klasse verwendet HostnameVerifier und SSLSocketFactory. Für beide Klassen sind Standardimplementierungen definiert. [ 1 ]
Bedeutet das, wenn Sie eine Verbindung mit öffnen?
httpsCon = (HttpsURLConnection) url.openConnection();
Es ist bereits SSL/TLS-verschlüsselt, ohne weitere Probleme?
Wie kann ich die TLS-Version für die Standardimplementierung anzeigen und festlegen? (Sollte TLS 1.2 für Java 8 und TLS 1.0 für Java 7) sein.)
Sie müssen ein SSLContext
erstellen, um das Protokoll festzulegen:
in Java 1.8:
SSLContext sc = SSLContext.getInstance("TLSv1.2");
// Init the SSLContext with a TrustManager[] and SecureRandom()
sc.init(null, trustCerts, new Java.security.SecureRandom());
in Java 1.7:
SSLContext sc = SSLContext.getInstance("TLSv1");
// Init the SSLContext with a TrustManager[] and SecureRandom()
sc.init(null, trustCerts, new Java.security.SecureRandom());
dann müssen Sie nur den SSLContext auf HttpsURLConnection setzen:
httpsCon.setSSLSocketFactory(sc.getSocketFactory());
Das sollte den Trick machen.
Sie können das TLS 1.2-Protokoll auch mit dem JDK 1.7 einstellen. Standardmäßig setzt JDK 1.7 den Wert auf 1.0.
SSLContext sc = SSLContext.getInstance("TLSv1.2"); //$NON-NLS-1$
sc.init(null, null, new Java.security.SecureRandom());
HttpsURLConnection con = (HttpsURLConnection) httpsURL.openConnection();
con.setSSLSocketFactory(sc.getSocketFactory());
private static javax.net.ssl.SSLSocketFactory getFactorySimple()
throws Exception {
SSLContext context = SSLContext.getInstance("TLSv1.2");`
context.init(null, null, null);
return context.getSocketFactory();
}
String loginurl ="some url";
HttpsURLConnection connection = null;
URL url = new URL(loginURL);
connection = (HttpsURLConnection) url.openConnection();
javax.net.ssl.SSLSocketFactory sslSocketFactory =getFactorySimple();
connection.setSSLSocketFactory(sslSocketFactory);
Mit dem obigen Code können Sie tls 1.1 oder tls 1.2 in Java 1.7 aktivieren