Java SSL & HTTPS
In this page:
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
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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)");
}
}
Login to try C/C++/Java/PHP code in the editor
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
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");
}
}
Login to try C/C++/Java/PHP code in the editor
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
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 try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 19 topics to unlock
0/19 topics done
Complete these topics first:
- Java Reflection API
- Java Annotations Advanced
- Java Garbage Collection
- Java Memory Management
- Java Performance Optimization
- Java Advanced Interview Questions
- Java CompletableFuture
- Java Atomic Classes
- Java Locks & Semaphores
- Java Concurrent Collections
- Java Cryptography Basics
- Java Hashing (MD5, SHA)
- Java SSL & HTTPS
- Java Logging (Log4j/SLF4J)
- Java Serialization Advanced
- Java Interview Questions Advanced
- Java Connection Pooling
- Java Test Driven Development
- Java Integration Testing