在Java中有如下方式支持配置TLS的版本:
方式一:
System.setProperty("https.protocols", "TLSv1");
System.setProperty("jdk.tls.client.protocols", "TLSv1");
方式二:
HttpsURLConnection connection = (HttpsURLConnection) this.url.openConnection();
SSLContext sc = SSLContext.getInstance("TLSv1.2");
sc.init(null, null, new java.security.SecureRandom());
connection.setSSLSocketFactory(sc.getSocketFactory());
logger.info("---SSLContext sc " + sc.getProtocol());
注意:如果当前进程中有一个地方已经按照方式一设置了之后,那么其他地方再通过方式二设置将不会生效;如果不设置TLS的版本号,客户端可能得到如下连接被重置的报错,eg:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:197) ~[na:1.7.0_161]
at java.net.SocketInputStream.read(SocketInputStream.java:122) ~[na:1.7.0_161]
at sun.security.ssl.InputRecord.readFully(InputRecord.java:442) ~[na:1.7.0_161]
at sun.security.ssl.InputRecord.read(InputRecord.java:480) ~[na:1.7.0_161]
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:946) ~[na:1.7.0_161]
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1344) ~[na:1.7.0_161]
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371) ~[na:1.7.0_161]
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355) ~[na:1.7.0_161]
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559) ~[na:1.7.0_161]
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) ~[na:1.7.0_161]
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:162) ~[na:1.7.0_161]
at com.ztesoft.zsmart.provisioning.plugins.sa.protocols.ProtoSoapHttpSaferpay4POST.write(ProtoSoapHttpSaferpay4POST.java:113) ~[prov_adapters_project.jar:na]
at com.ztesoft.zsmart.provisioning.plugins.sa.protocols.ProtoSoapHTTPBase.buildPkt(ProtoSoapHTTPBase.java:138) [prov_adapters.jar:na]
at com.ztesoft.zsmart.provisioning.core.serviceactive.NetworkElementThread.doNEInteractiveSend(NetworkElementThread.java:403) [prov_core.jar:na]
at com.ztesoft.zsmart.provisioning.core.serviceactive.NetworkElementThread.doWork(NetworkElementThread.java:1015) [prov_core.jar:na]
at com.ztesoft.zsmart.provisioning.core.common.WorkThread.threadFun(WorkThread.java:428) [prov_core.jar:na]
at com.ztesoft.zsmart.provisioning.core.common.WorkThread$1.run(WorkThread.java:389) [prov_core.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.7.0_161]