Skip to content

Instantly share code, notes, and snippets.

@michalbcz
Created May 14, 2018 13:01
Show Gist options
  • Save michalbcz/1d397cb434c0a17d61acc9a60c0a0ab9 to your computer and use it in GitHub Desktop.
Save michalbcz/1d397cb434c0a17d61acc9a60c0a0ab9 to your computer and use it in GitHub Desktop.
Java/Groovy disable SSL verification
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = [ 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);
def url = new URL("https://www.google.com")
println url.getText()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment