0


unable to find valid certification path to requested target

调用https接口时出现该异常,

Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

原因是可以看上图,因为本地没有目标服务器证书导致。解决此方法的两种方案,1.在运行java环境安装对方服务器证书,可使用keytool -printcert -rfc -sslserver ip:port 后保存为cer格式。2.在http建立连接时跳过证书验证

CloseableHttpClient httpClient = getScontractHttpClient();

public static CloseableHttpClient getScontractHttpClient() {
        SSLContext sslContext = null;
        try {
            sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            }).build();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        //创建httpClient
        return HttpClients.custom().setSSLContext(sslContext).
                setSSLHostnameVerifier(new NoopHostnameVerifier()).build();

    }
    
标签: 服务器 https

本文转载自: https://blog.csdn.net/weixin_42740540/article/details/128128394
版权归原作者 185的阿平 所有, 如有侵权,请联系我们删除。

“unable to find valid certification path to requested target”的评论:

还没有评论