平台服务调用https接口报错:
org.springframework.web.client.ResourceAccessException: I/0 error on PoST request for ?"https://XXXXX": java.security.centp.CertificateException: No subject alternative names present; nested exception is javax.net.ssl..SSLHandshakeException: java.security.cert.CertificateException: No subject alternative namesspresent
第一种方法:配置相关SSL证书到服务器
第二种方法:如果没有相关服务器权限,又想快速验证接口调用,可以在请求时添加跳过SSL证书,可以 快捷实现,当然生产环境还是建议配置证书方式,降低风险
public static String sendAskSkipSSLCertificate(String url, Map body, Map header) throws Exception { CloseableHttpResponse response = null; // 处理请求路径 url = UriComponentsBuilder.fromHttpUrl(url) .toUriString(); //创建httpclient对象 CloseableHttpClient client = null; String respBody; client = HttpClients.custom().setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContexts.custom() .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE)).build(); //创建post方式请求对象 HttpPost httpPost = new HttpPost(url); // 请求头设置 httpPost.setHeader("Content-Type", "application/json"); if (header != null) { for (String s : header.keySet()) { httpPost.setHeader(s, header.get(s)); } } if (body != null) { httpPost.setEntity(new StringEntity(JSON.toJSONString(body), "utf-8")); } response = client.execute(httpPost); org.apache.http.HttpEntity entity = response.getEntity(); if (entity != null) { respBody = EntityUtils.toString(entity); return respBody; } return null; }
来源地址:https://blog.csdn.net/Zxiaobinggan/article/details/129306508