Created
April 28, 2015 06:29
-
-
Save oldratlee/e08fab8c1fead27042af to your computer and use it in GitHub Desktop.
Disable Ssl Verification
This file contains hidden or 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
static boolean sslVerificationDisabled = false; | |
public static synchronized void disableSslVerification() { | |
if (sslVerificationDisabled) return; | |
try { | |
// Create a trust manager that does not validate certificate chains | |
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { | |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
public void checkClientTrusted(X509Certificate[] certs, String authType) { | |
} | |
public void checkServerTrusted(X509Certificate[] certs, String authType) { | |
} | |
} | |
}; | |
// Install the all-trusting trust manager | |
SSLContext sc = SSLContext.getInstance("SSL"); | |
sc.init(null, trustAllCerts, new java.security.SecureRandom()); | |
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); | |
// Create all-trusting host name verifier | |
HostnameVerifier allHostsValid = new HostnameVerifier() { | |
public boolean verify(String hostname, SSLSession session) { | |
return true; | |
} | |
}; | |
// Install the all-trusting host verifier | |
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); | |
} catch (Exception e) { | |
throw new IllegalStateException("Fail to disableSslVerification, cause: " + e.toString(), e); | |
} | |
sslVerificationDisabled = true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello my friend
i am from Brasil, your solution is the best, congratulations to you
how to disable ssl verification to an specific URL?