对接第三方接口时一直提示SSL证书校验异常,可通过下面的方法绕过校验:
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
HttpPost httpPost = new HttpPost(eqsUrl);
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(body_bytes);
httpPost.setEntity(byteArrayEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
String res = EntityUtils.toString(response.getEntity(), "GBK");