Java SSL और HTTPS
In this page:
SSL/TLS Overview
SSL (Secure Sockets Layer) और इसका आधुनिक successor TLS (Transport Layer Security) किसी client और server के बीच network communication को encrypt करते हैं, network पार करते समय transit में data को eavesdropping या tampering से बचाते हुए।
उदाहरण: 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 आपको specifically HTTPS पर requests भेजने देता है, यह सुनिश्चित करते हुए कि किसी domain (उदाहरण के लिए cookiescursor.com) के साथ exchanged data end-to-end encrypted है बजाय इसके कि एक plain HttpURLConnection HTTP पर इसे plaintext के रूप में भेजे।
उदाहरण: 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
एक TrustManager TLS handshake के दौरान यह तय करने के लिए ज़िम्मेदार है कि किसी server के SSL certificate पर भरोसा किया जाए या नहीं। Custom TrustManagers कभी-कभी test environments में self-signed certificates को accept करने के लिए उपयोग होते हैं, लेकिन production में कभी सभी certificates पर अंधाधुंध भरोसा करने के लिए उपयोग नहीं होने चाहिए।
उदाहरण: 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 आपको किसी client और server के बीच सीधे एक सुरक्षित, encrypted raw TCP connection खोलने का एक lower-level तरीका देता है, तब उपयोगी जब आपको किसी higher-level HTTP client के बजाय socket level पर TLS चाहिए।
उदाहरण: SSLSocket
// Import javax.net.ssl.* so it can be used by its short name
import javax.net.ssl.*;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) throws Exception {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
// Print a line to the console
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
हमेशा अपने SSLContext को TLSv1.3 जैसे modern, secure TLS versions enforce करने और पुराने, vulnerable protocol versions (जैसे SSLv3 या TLS 1.0) को reject करने के लिए configure करें, क्योंकि किसी पुराने protocol में वापस आने वाले connections TLS के देने वाले security guarantees खो देते हैं।
उदाहरण: 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
- कोई error हटाने के लिए हर certificate को accept करने वाला TrustManager लिखना, जो HTTPS की दी protection हटा देता है।
- SSLv3 या TLS 1.0 जैसे पुराने protocols को enable करना, जबकि सिर्फ TLSv1.2 और TLSv1.3 जैसे modern versions की अनुमति होनी चाहिए।
http://URL से खोले गए connection कोHttpsURLConnectionमें cast करना, जोClassCastExceptionफेंकता है।
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