一、场景
近期在对接第三方接口时,通过HttpClient发送Https请求报 : unable to find valid certification path to requested target(请求的地址需要校验SSL)。
二、详细报错信息
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
三、解决方案
HttpClient配置绕过SSL的检验即可!!!
代码:
// 解决httpClient发送https错误的问题
SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(
SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),
NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(scsf).build();
HttpPost httpPost = new HttpPost("请求地址");
httpPost.setHeader("Authorization", "校验串");
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
JSONObject object = JSON.parseObject(EntityUtils.toString(responseEntity));