Forked from mariuszprzydatek/SSLCertificateValidation.java
Created
September 8, 2021 21:46
-
-
Save sunil-bagde/6097a4ad5f0fee36a2a4c8fe9f29cc4f to your computer and use it in GitHub Desktop.
mariuszprzydatek.com blog example on how to disable SSL certificate validation in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package my.hydepark.ssl; | |
import javax.net.ssl.*; | |
import java.security.cert.CertificateException; | |
import java.security.cert.X509Certificate; | |
public class SSLCertificateValidation { | |
public static void disable() { | |
try { | |
SSLContext sslc = SSLContext.getInstance("TLS"); | |
TrustManager[] trustManagerArray = { new NullX509TrustManager() }; | |
sslc.init(null, trustManagerArray, null); | |
HttpsURLConnection.setDefaultSSLSocketFactory(sslc.getSocketFactory()); | |
HttpsURLConnection.setDefaultHostnameVerifier(new NullHostnameVerifier()); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private static class NullX509TrustManager implements X509TrustManager { | |
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { | |
System.out.println(); | |
} | |
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { | |
System.out.println(); | |
} | |
public X509Certificate[] getAcceptedIssuers() { | |
return new X509Certificate[0]; | |
} | |
} | |
private static class NullHostnameVerifier implements HostnameVerifier { | |
public boolean verify(String hostname, SSLSession session) { | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment