我有以下代码有条件地(基于 a boolean
)禁用 SSL 证书检查。
但是,如果我设置boolean
为false
并重新运行我的代码,SSL 检查似乎仍然被禁用(当它应该重新启用时)。
那么,与此相反的逻辑是什么,从而恢复了检查呢?
if (bIgnoreSSL) {
TrustManager[] trustAllCertificates = new TrustManager[] {
new X509TrustManager()
{
@Override
public X509Certificate[] getAcceptedIssuers() { return null; // Not relevant.}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) { // Do nothing. Just allow them all. }
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType){ // Do nothing. Just allow them all.}
}
};
HostnameVerifier trustAllHostnames = new HostnameVerifier()
{
@Override
public boolean verify(String hostname, SSLSession session) { return true; // Just allow them all. }
};
try
{
System.setProperty("jsse.enableSNIExtension", "false");
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCertificates, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(trustAllHostnames);
}
catch (GeneralSecurityException e)
{
throw new ExceptionInInitializerError(e);
}
}
else {
// Code to restore here (Opposite of above?)
}