Java 8 SSLContext.getInstance(“TLSv1.2”) what does it mean?

非 Y 不嫁゛ 提交于 2020-01-15 03:27:28

问题


I'm trying to call a REST API for some end point URL, it was running fine for the URL of java.net and after executing the same. But on some machines it failed for some SSLException so the code was modified like this:

HttpsURLConnection connection = (HttpsURLConnection) new URL( url ).openConnection()

SSLContext sc = SSLContext.getInstance("TLSv1.2")
sc.init(null, null, new SecureRandom())
connection.setSSLSocketFactory(sc.getSocketFactory())
connection.setRequestProperty("charset", "utf-8")

InputStream input = connection.getInputStream()
InputStreamReader reader = new InputStreamReader(input, "utf-8")
BufferedReader buffer = new BufferedReader(reader)

buffer.lines().collect(Collectors.joining("\n"))

Now again it's working fine, if I'm not wrong it is for secured communication, but then why was the SSLexception on Linux env and not on Windows, and SSLContext.getInstance("TLSv1.2") will this work on all environments, why not TLSV1.1 does all environment have 1.2 or latest version by default, how do I make sure that it will run on all the environments?


回答1:


TLS stands for Transport Level Security which is the standard superseding Secure Sockets Layer (SSL). There are few versions of TLS out of which v1.1 and v1.2 are consider secure (at the moment v1.3 is being drafted). If you don't want to dive into technical details using the latest available TLS version is the best idea.

TLSv1.2 in Java is nicely explained here: JDK 8 will use TLS 1.2 as default. Since TLSv1.2 is implemented inside the JDK it should work on all operating systems. Since there are multiple JDK vendors (Oracle, OpenJDK, Azul, etc.) you want to test this with your JDK. The gotcha is that older version e.g. Oracle HotSpot Java 6 support only SSL and you won't get TLS unless you have premium Oracle maintenance release or custom libraries.



来源:https://stackoverflow.com/questions/49274172/java-8-sslcontext-getinstancetlsv1-2-what-does-it-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!