Skip to content

Instantly share code, notes, and snippets.

@oldratlee
Created April 28, 2015 06:29
Show Gist options
  • Save oldratlee/e08fab8c1fead27042af to your computer and use it in GitHub Desktop.
Save oldratlee/e08fab8c1fead27042af to your computer and use it in GitHub Desktop.
Disable Ssl Verification
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;
}
@dalvan-bevilaqua
Copy link

hello my friend
i am from Brasil, your solution is the best, congratulations to you
how to disable ssl verification to an specific URL?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment