Created
July 12, 2013 18:48
-
-
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 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
/* | |
** 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