← Back to Advanced Java Course | Chapter 11: Advanced & Security | Lesson 13 of 19

Java SSL & HTTPS

SSL/TLS Overview

SSL (Secure Sockets Layer) and its modern successor TLS (Transport Layer Security) encrypt network communication between a client and a server, protecting data in transit from eavesdropping or tampering as it crosses the network.

Example: SSL/TLS Overview

java
import javax.net.ssl.SSLContext;
public class Main {
	public static void main(String[] args) throws Exception {
		SSLContext context = SSLContext.getInstance("TLSv1.3"); // encrypts data in transit
		System.out.println(context.getProtocol());
	}
}

Creating HttpsURLConnection

HttpsURLConnection lets you send requests over HTTPS specifically, ensuring that data exchanged with a domain (for example cookiescursor.com) is encrypted end-to-end rather than sent as plaintext the way a plain HttpURLConnection over HTTP would.

Example: Creating HttpsURLConnection

java
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class Main {
	public static void main(String[] args) throws Exception {
		URL url = new URL("https://cookiescursor.com");
		HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); // encrypted, not plaintext HTTP
		System.out.println(conn.getURL());
	}
}

Custom TrustManagers

A TrustManager is responsible for deciding whether a server's SSL certificate should be trusted during the TLS handshake. Custom TrustManagers are sometimes used to accept self-signed certificates in test environments, but should never be used to blindly trust all certificates in production.

Example: Custom TrustManagers

java
import javax.net.ssl.*;
import java.security.cert.X509Certificate;
public class Main {
	public static void main(String[] args) throws Exception {
		TrustManager trustManager = new X509TrustManager() {
			public void checkClientTrusted(X509Certificate[] chain, String authType) {}
			public void checkServerTrusted(X509Certificate[] chain, String authType) {} // only for test environments
			public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
		};
		System.out.println("Custom TrustManager created (never blindly trust all certs in production)");
	}
}

SSLSocket

SSLSocket gives you a lower-level way to open a secure, encrypted raw TCP connection directly between a client and a server, useful when you need TLS at the socket level rather than going through a higher-level HTTP client.

Example: SSLSocket

java
import javax.net.ssl.*;
public class Main {
	public static void main(String[] args) throws Exception {
		SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
		System.out.println(factory.getClass().getSimpleName() + " creates raw encrypted sockets");
	}
}

Enforcing Secure TLS Versions

Always configure your SSLContext to enforce modern, secure TLS versions like TLSv1.3 and reject older, vulnerable protocol versions (such as SSLv3 or TLS 1.0), since connections that fall back to an outdated protocol lose the security guarantees TLS is supposed to provide.

Example: Enforcing Secure TLS Versions

java
import javax.net.ssl.SSLContext;
public class Main {
	public static void main(String[] args) throws Exception {
		SSLContext context = SSLContext.getInstance("TLSv1.3"); // rejects outdated protocols like SSLv3
		context.init(null, null, null);
		System.out.println("Enforcing " + context.getProtocol());
	}
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.