直接上代码啦!!!
/**
* 绕过验证 (不足:同时会影响整个tomcat里面对证书的验证)
*
* MyEclipse Struts Creation date: 2018-8-9
*
* @author
*
*/
public class SSL {
public static void disableChecks() {
/*try {
new URL("https://0.0.0.0/").getContent();
} catch (IOException e) {
// This invocation will always fail, but it will register the
// default SSL provider to the URL class.
}*/
try {
SSLContext sslc;
sslc = SSLContext.getInstance("TLS");
TrustManager[] trustManagerArray = { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
// 检查客户端证书信任
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
// 检查服务端证书验证
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
} };
//初始化SSLContext实例
sslc.init(null, trustManagerArray, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslc
.getSocketFactory());
//取消对证书的所有验证
HttpsURLConnection
.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String arg0, SSLSession arg1) {
return false;
}
});
} catch (Exception e) {
throw new IllegalArgumentException("证书校验异常!");
}
}
}