其他分享
首页 > 其他分享> > 异常解决:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.cer

异常解决:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.cer

作者:互联网

目标站点启用了HTTPS 而缺少安全证书时出现的异常,大概解决办法有2种:

  1. 手动导入安全证书(嫌麻烦 没使用);

  2. 忽略证书验证。

// 包不要导错了
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* 信任任何站点,实现https页面的正常访问
*/

public static void trustEveryone() {

try {
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new X509TrustManager[]{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];
            }
        }}, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

 

标签:import,sun,void,X509Certificate,path,new,security,public
来源: https://www.cnblogs.com/WilsonEdwards/p/16351242.html