Created
November 23, 2012 15:13
-
-
Save ctomc/4136081 to your computer and use it in GitHub Desktop.
Trust all certs
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
if (con instanceof HttpsURLConnection) { | |
log.info("setting non validating ssl socket factory"); | |
HttpsURLConnection ssl = (HttpsURLConnection) con; | |
TrustAllSecurityManager.setAllTrustingValidators(ssl); | |
} | |
public class TrustAllSecurityManager { | |
private static final Logger log = LoggerFactory.getLogger(TrustAllSecurityManager.class); | |
private static SSLSocketFactory instance = null; | |
private static SSLContext sslContext = null; | |
public static SSLContext getSslContextInstance() { | |
if (sslContext == null) { | |
try { | |
SSLContext sc = SSLContext.getInstance("SSL"); | |
sc.init(null, new TrustManager[]{new X509TrustManager() { | |
public X509Certificate[] getAcceptedIssuers() { | |
log.debug("getAcceptedIssuers ============="); | |
return null; | |
} | |
public void checkClientTrusted(X509Certificate[] certs, | |
String authType) { | |
log.debug("checkClientTrusted ============="); | |
} | |
public void checkServerTrusted(X509Certificate[] certs, | |
String authType) { | |
log.debug("checkServerTrusted ============="); | |
} | |
}}, new SecureRandom()); | |
return sc; | |
} catch (Exception e) { | |
log.warn("could not create ssl context",e); | |
} | |
} | |
return sslContext; | |
} | |
public static SSLSocketFactory instance() { | |
if (instance == null) { | |
// Install the all-trusting trust manager | |
try { | |
instance = getSslContextInstance().getSocketFactory(); | |
//HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); | |
} catch (Exception e) { | |
return null; | |
} | |
} | |
return instance; | |
} | |
public static void setAllTrustingValidators(HttpsURLConnection ssl) { | |
ssl.setSSLSocketFactory(instance()); | |
ssl.setHostnameVerifier(new TrustAllHostNameVerifier()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment