Java HttpsURLConnection和TLS 1.2 [英] Java HttpsURLConnection and TLS 1.2

查看:1371
本文介绍了Java HttpsURLConnection和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)

  1. Oracle公司(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆