Mar*_*ann 25 java ssl httpsurlconnection
我在一篇文章中读到了HttpsURLConnection 透明地协商SSL连接的文章.
官方文件说:
该类使用HostnameVerifier和SSLSocketFactory.为这两个类定义了默认实现.[ 1 ]
这是否意味着一旦你打开一个连接
httpsCon = (HttpsURLConnection) url.openConnection();
Run Code Online (Sandbox Code Playgroud)
它已经加密了SSL/TLS而没有任何麻烦吗?
如何查看和设置标准实现的TLS版本?(应该是Java 8的TLS 1.2和Java 7的TLS 1.0)
小智 29
您必须创建一个SSLContext以
在Java 1.8中设置Protocoll :
SSLContext sc = SSLContext.getInstance("TLSv1.2");
// Init the SSLContext with a TrustManager[] and SecureRandom()
sc.init(null, trustCerts, new java.security.SecureRandom());
Run Code Online (Sandbox Code Playgroud)
在Java 1.7中:
SSLContext sc = SSLContext.getInstance("TLSv1");
// Init the SSLContext with a TrustManager[] and SecureRandom()
sc.init(null, trustCerts, new java.security.SecureRandom());
Run Code Online (Sandbox Code Playgroud)
那么你只需要将SSLContext设置为HttpsURLConnection:
httpsCon.setSSLSocketFactory(sc.getSocketFactory());
Run Code Online (Sandbox Code Playgroud)
这应该够了吧.
Kon*_*aka 10
您还可以使用JDK 1.7设置TLS 1.2协议.默认情况下,JDK 1.7将其设置为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());
Run Code Online (Sandbox Code Playgroud)