Skip to content

Instantly share code, notes, and snippets.

@barbietunnie
Created August 25, 2015 10:26
Show Gist options
  • Save barbietunnie/26e21b70f2986c781f10 to your computer and use it in GitHub Desktop.
Save barbietunnie/26e21b70f2986c781f10 to your computer and use it in GitHub Desktop.
public void doTrustToCertificates() throws Exception {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
return;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
return;
}
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
logger.info("+++ URL host name: " + urlHostName + ", peer host: " + session.getPeerHost() + " +++");
if (!urlHostName.equalsIgnoreCase(session.getPeerHost())) {
System.out.println("Warning: URL host '" + urlHostName + "' is different to SSLSession host '" + session.getPeerHost() + "'.");
}
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment