調(diào)用https接口時出現(xiàn)該異常,
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
?文章來源:http://www.zghlxwxcb.cn/news/detail-783586.html
原因是可以看上圖,因?yàn)楸镜貨]有目標(biāo)服務(wù)器證書導(dǎo)致。解決此方法的兩種方案,1.在運(yùn)行java環(huán)境安裝對方服務(wù)器證書,可使用keytool?-printcert?-rfc?-sslserver?ip:port 后保存為cer格式。2.在http建立連接時跳過證書驗(yàn)證文章來源地址http://www.zghlxwxcb.cn/news/detail-783586.html
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();
}
//創(chuàng)建httpClient
return HttpClients.custom().setSSLContext(sslContext).
setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
}
到了這里,關(guān)于unable to find valid certification path to requested target的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!