Skip to content

Instantly share code, notes, and snippets.

@linusfoldemo
Created March 3, 2016 10:13
Show Gist options
  • Save linusfoldemo/449e0af7bbc3fed0fabf to your computer and use it in GitHub Desktop.
Save linusfoldemo/449e0af7bbc3fed0fabf to your computer and use it in GitHub Desktop.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class SSLCertificateValidation {
private static final Logger logger = LoggerFactory.getLogger(SSLCertificateValidation.class);
public static void disable() {
try {
logger.warn("Disabling SSl certification validation!");
SSLContext sslc = SSLContext.getInstance("TLS");
TrustManager[] trustManagerArray = { new NullX509TrustManager() };
sslc.init(null, trustManagerArray, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslc.getSocketFactory());
} catch(Exception e) {
e.printStackTrace();
}
}
private static class NullX509TrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//noop
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//noop
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment