package com.sinitek.sirm.busin.ipa.utils;
import org.apache.log4j.Logger;
import javax.net.ssl.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public final class DisableSSLCertificateCheckUtil {
private static final Logger LOGGER = Logger.getLogger(DisableSSLCertificateCheckUtil.class);
/**
* Prevent instantiation of utility class.
*/
private DisableSSLCertificateCheckUtil() {
}
/**
* 1.取消对https证书的所有验证
* Disable trust checks for SSL connections.
* 通过重写TrustManager的checkClientTrusted(检查客户端证书信任)和checkServerTrusted(检查服务端证书验证)。
* 以及HostnameVerifier的verify(校验)方法即可取消对https证书的所有验证。
* <p>
* 2. 影响的范围:将会影响整个tomcat里面对证书的验证。即通过tomcat里面的其他项目虽然没有执行这一段代码但是也同样会忽略证书的验证。
* 3. 影响的时间:执行这段代码之后的所有时间都生效。
*/
public static void disableChecks() {
try {
SSLContext sslc;
sslc = SSLContext.getInstance("TLS");
TrustManager[] trustManagerArray = {new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}};
sslc.init(null, trustManagerArray, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
});
} catch (Exception e) {
LOGGER.error("error msg:{}", e);
throw new IllegalArgumentException("证书校验异常!");
}
}
}
取消对https证书的所有验证
最新推荐文章于 2024-04-16 19:57:39 发布