Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to TLS 1.2 enabled URL with Java [closed]

Tags:

ssl

tls1.2

How to connect to TLS 1.2 enabled URL using HTTP POST Method.

like image 774
bookofcodes Avatar asked Jun 27 '17 19:06

bookofcodes


People also ask

How do I enable TLS 1.2 in Java?

If your application runs on Java 1.7 or Java 1.6 (update 111 or later), you can set the https. protocols system property when starting the JVM to enable additional protocols for connections made using the HttpsURLConnection class – for example, by setting -Dhttps. protocols=TLSv1. 2 .

Is TLS 1.2 automatically enabled?

TLS 1.2 is automatically enabled in Google Chrome version 29 or greater.


1 Answers

Java 8

Java 8 will use TLS 1.2 by default

https://blogs.oracle.com/java-platform-group/jdk-8-will-use-tls-12-as-default

So for Java 8 all you need to do is the following.

import javax.net.ssl.*;
import java.net.URL;

URL url = new URL("https://www.google.com");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

Java 7

Java 7 needs to be set manually

import java.security.*;
import javax.net.ssl.*;
import java.net.URL;


URL url = new URL("https://www.google.com");

SSLContext ssl = SSLContext.getInstance("TLSv1.2"); 
ssl.init(null, null, new SecureRandom());

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setSSLSocketFactory(ssl.getSocketFactory());
like image 56
Chris Maggiulli Avatar answered Sep 27 '22 21:09

Chris Maggiulli