Skip to content

Instantly share code, notes, and snippets.

@netoht
Last active August 29, 2015 14:22
Show Gist options
  • Save netoht/2a537d27cd1c250d4d6d to your computer and use it in GitHub Desktop.
Save netoht/2a537d27cd1c250d4d6d to your computer and use it in GitHub Desktop.
SkipSSL - ignoreSSLCertificate
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class SkipSSL {
public final static TrustManager[] TRUST_ALL_CERTS = new TrustManager[] { new X509TrustManager() {
@Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; }
@Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
@Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
}};
public static void ignoreSSLCertificate() {
SSLContext sc = createSSLContextWithSSL();
SSLContext.setDefault(sc);
}
/**
* Create a SSL Context with trust manager that does not validate certificate chains
*/
public static SSLContext createSSLContextWithSSL() {
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, TRUST_ALL_CERTS, new java.security.SecureRandom());
return sc;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Create a SSL Context with trust manager that does not validate certificate chains
*/
public static SSLContext createSSLContextWithTLS() {
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, TRUST_ALL_CERTS, null);
return sc;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// private static DefaultHttpClient newHttpClientAvoidSSL(long timeout, TimeUnit timeUnit, int retries) throws GeneralSecurityException {
// SSLContext sslContext = SSLContext.getInstance("TLS");
// sslContext.init(null, new TrustManager[]{
// new X509TrustManager() {
// public X509Certificate[] getAcceptedIssuers() {
// return null;
// }
//
// public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
//
// public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
// }
// }, null);
// SSLSocketFactory ssf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
// ClientConnectionManager connectionManager = new DefaultHttpClient().getConnectionManager();
// connectionManager.getSchemeRegistry().register(new Scheme("https", 443, ssf));
//
// DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, createBasicHttpParams(timeout, timeUnit));
// httpClient.setHttpRequestRetryHandler(new RetryHandler(retries));
//
// return httpClient;
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment