Skip to content

Instantly share code, notes, and snippets.

@miketihonchik
Created July 12, 2013 18:48
Show Gist options
  • Save miketihonchik/5986793 to your computer and use it in GitHub Desktop.
Save miketihonchik/5986793 to your computer and use it in GitHub Desktop.
this is fix for "famous" javax.net.ssl.SSLHandshakeException: unable to find valid certification path to requested target it's OK to use this for testing, but not production
/*
** this is fix for "famous" javax.net.ssl.SSLHandshakeException:
** unable to find valid certification path to requested target
** it's OK to use this for testing, but not production
*/
public static void disableCertificateValidation() {
// Create new Trust Manager that will not validate any of the certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {}
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
}};
// Ignore differences in hostname, wherever certificate hostname or not
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) { return true; }
};
// Implement our Trust Manager that will trust "everyone"
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
} catch (Exception e) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment