First, we create stub implementations of HostnameVerifier and X509TrustManager.
public static class NullVerifier implements HostnameVerifier {
@Override
public final boolean verify(final String hostname,
final SSLSession sslSession) {
return true;
}
}
public static class NullTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(final X509Certificate[] chain,
final String authType)
throws CertificateException {
}
@Override
public void checkServerTrusted(final X509Certificate[] chain,
final String authType)
throws CertificateException {
}
@Override
public final X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
Then, we install our stub implementations to the SSL connection.
// Configure SSL Context
SSLContext sslContext = SSLContext.getInstance("TLS");
X509TrustManager nullTrustManager = new NullTrustManager();
TrustManager[] nullTrustManagers = {nullTrustManager};
sslContext.init(null, nullTrustManagers, new SecureRandom());
// Create HTTPS connection
URL url = new URL("https", "127.0.0.1", 8443, "/ssltest");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(new NullVerifier());
conn.setSSLSocketFactory(sslContext.getSocketFactory());
No comments:
Post a Comment