Java HttpsURLConnection 和 TLS 1.2 [英] Java HttpsURLConnection and TLS 1.2
问题描述
我在一篇文章中读到 HttpsURLConnection
将透明地协商 SSL 连接.
I read in an article that HttpsURLConnection
will transparently negotiate the SSL connection.
官方文档说:
该类使用 HostnameVerifier 和 SSLSocketFactory.这两个类都定义了默认实现.[1]
This class uses HostnameVerifier and SSLSocketFactory. There are default implementations defined for both classes. [1]
这是否意味着一旦您打开与
Does that mean once you open a connection with
httpsCon = (HttpsURLConnection) url.openConnection();
它已经被 SSL/TLS 加密了,没有任何麻烦?
It is already SSL/TLS encrypted without any more hassle?
如何查看和设置标准实现的 TLS 版本?(对于 Java 8 应该是 TLS 1.2,对于 Java 7 应该是 TLS 1.0)
How can I view and set the TLS version for the standard implementation? (Should be TLS 1.2 for Java 8 and TLS 1.0 for Java 7)
- 甲骨文公司(2011 年).javax.net.ssl.HttpsURLConnection.(JavaDoc)
推荐答案
您必须创建一个 SSLContext
来设置 Protocoll:
在 Java 1.8 中:
You will have to create an SSLContext
to set the Protocoll:
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());
在 Java 1.7 中:
SSLContext sc = SSLContext.getInstance("TLSv1");
// Init the SSLContext with a TrustManager[] and SecureRandom()
sc.init(null, trustCerts, new java.security.SecureRandom());
然后您只需将 SSLContext 设置为 HttpsURLConnection:
then you just have to set the SSLContext to the HttpsURLConnection:
httpsCon.setSSLSocketFactory(sc.getSocketFactory());
那应该就行了.
这篇关于Java HttpsURLConnection 和 TLS 1.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!