Skip to content

Instantly share code, notes, and snippets.

@michalbcz
Last active December 11, 2020 07:56
Show Gist options
  • Save michalbcz/f8e7fcdc2fe17605d3966f17956c40c0 to your computer and use it in GitHub Desktop.
Save michalbcz/f8e7fcdc2fe17605d3966f17956c40c0 to your computer and use it in GitHub Desktop.
turn off SSL verification #2
// methods
private static SSLSocketFactory createSslSocketFactory(X509TrustManager trustManager) {
try {
final SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new TrustManager[]{trustManager}, new SecureRandom());
return context.getSocketFactory();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
return ExceptionUtils.rethrow(e);
}
}
private static X509TrustManager trustAllX509TrustManager() {
return new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) { }
public void checkServerTrusted(X509Certificate[] chain, String authType) { }
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
};
}
// then used in code somewhere
X509TrustManager trustManager = trustAllX509TrustManager();
SSLSocketFactory socketFactoryWithoutVerification = createSslSocketFactory(trustManager);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment