Created
September 6, 2013 10:51
-
-
Save trastle/6462307 to your computer and use it in GitHub Desktop.
Disable SSL Checks
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 com.github.styx.config; | |
import javax.net.ssl.*; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.security.KeyManagementException; | |
import java.security.cert.CertificateException; | |
import java.security.cert.X509Certificate; | |
import java.security.NoSuchAlgorithmException; | |
public final class DisableSSLCheckUtil { | |
/** | |
* Prevent instantiation of utility class. | |
*/ | |
private DisableSSLCheckUtil() { | |
} | |
/** | |
* Trust manager that does not perform nay checks. | |
*/ | |
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]; | |
} | |
} | |
/** | |
* Host name verifier that does not perform nay checks. | |
*/ | |
private static class NullHostnameVerifier implements HostnameVerifier { | |
public boolean verify(String hostname, SSLSession session) { | |
return true; | |
} | |
} | |
/** | |
* Disable trust checks for SSL connections. | |
*/ | |
public static void disableChecks() throws NoSuchAlgorithmException, | |
KeyManagementException { | |
try { | |
new URL("https://0.0.0.0/").getContent(); | |
} catch (IOException e) { | |
// This invocation will always fail, but it will register the | |
// default SSL provider to the URL class. | |
} | |
try{ | |
SSLContext sslc; | |
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment