最近使用HttpClient对接第三方短信接口,在进行本地测试时报了一个证书失效的错误。

1. 封装的HttpClient的Post请求
public static Map<String, Object> postReq(String URL, Map<String, Object> paramMap, Map<String, String> headers) throws Exception {Map<String, Object> map = new HashMap<String, Object>();RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(2000) // 设置连接超时时间,单位毫秒.setConnectionRequestTimeout(1000).setSocketTimeout(5000) // 请求获取数据的超时时间,单位毫秒.build();HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {@Overridepublic boolean retryRequest(IOException exception, int executionCount, HttpContext context) {return false;}};try (CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig).setRetryHandler(myRetryHandler).build()) {HttpPost httpPost = new HttpPost(URL);if (paramMap != null) {JSONObject paramJson = new JSONObject(paramMap);StringEntity paramEntity = new StringEntity(paramJson.toString(), "UTF-8");paramEntity.setContentType("application/json; charset=utf-8");httpPost.setEntity(paramEntity);}httpPost.setConfig(requestConfig);if (headers != null && !headers.isEmpty()) {for (String key : headers.keySet()) {String value = headers.get(key);httpPost.setHeader(key, value);}}CloseableHttpResponse response = client.execute(httpPost);HttpEntity entity = response.getEntity();if (entity != null) {String responseStr = EntityUtils.toString(entity, "UTF-8");if (responseStr.isEmpty()) {responseStr = "{}";}int statusCode = response.getStatusLine().getStatusCode();if (HttpServletResponse.SC_OK == statusCode) {try {JSONObject dataJson = (JSONObject) JSONObject.parse(responseStr);map = new HashMap<>(dataJson);} catch (Exception e) {map.put("reponse", responseStr);}} else {return map;}}response.close();}return map;}

但是在访问一些自签的https请求时会报错,该问题是链接证书无效导致,原因是由于自签证书会被识别为不安全链接。

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

2. 解决方法

我试着将请求链接的证书文件添加到jdk的cer证书信任库中,但是没有产生效果。(短信接口的证书无效)报了另外一个异常:
javax.net.ssl.SSLPeerUnverifiedException: Certificate for doesn’t match any of the subject alternative names: []

查看了一些解决方法,也是比较多使用信任链接证书的方式忽略SSL的校验
https://stackoverflow.com/questions/39762760/javax-net-ssl-sslexception-certificate-doesnt-match-any-of-the-subject-alterna

查看了Apache HttpClient官方的回答
(https://issues.apache.org/jira/browse/HTTPCLIENT-2047)
应该是HttpClient本身的问题,4.5.10和4.5.11版本 DefaultHostnameVerifier 中的回归导致拒绝具有非标准域的证书。
需要信任证书来解决,问题在4.5.12解决,但是我升级了HttpClient的版本也没有效果(原版本4.5.10)

  1. 根据网上方法设置了jdk的安全证书-> 没有解决
  2. 设置了maven忽略证书校验->没有解决
  3. 升级HttpClient版本-> 没有解决
  4. yml文件配置httpclient忽略SSL校验,貌似也没有效果。
  5. 最后使用了修改代码的方法,原理也是忽略证书校验,但是代码产生了效果,估计是和构建的Httpclient有关

修改原代码

// 忽略SSL安全认证
**SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),NoopHostnameVerifier.INSTANCE);**try (CloseableHttpClient client = **HttpClients.custom().setSSLSocketFactory(scsf)**.setDefaultRequestConfig(requestConfig).setRetryHandler(myRetryHandler).build()) {

Java使用HttpClient发送Https请求证书失效:PKIX path building failed:相关推荐

  1. 【笔记】Java 信任所有SSL证书(解决PKIX path building failed问题)

    [笔记]Java 信任所有SSL证书(解决PKIX path building failed问题) 参考文章: (1)[笔记]Java 信任所有SSL证书(解决PKIX path building f ...

  2. HttpClient发送Https请求报 : unable to find valid certification path to requested target

    一.场景   近期在对接第三方接口时,通过HttpClient发送Https请求报 : unable to find valid certification path to requested tar ...

  3. https证书缺失报错:PKIX path building failed 问题解决

    背景: 当使用java来进行爬虫操作的时候,会遇到处理https站点时报错:PKIX path building failed: sun.security.provider.certpath.SunC ...

  4. 使用Java消费API的一个错误消息PKIX path building failed以及解决方案

    我使用Java代码消费一个网站的Restful API,遇到如下错误: PKIX path building failed: sun.security.provider.certpath.SunCer ...

  5. 用HttpClient发送HTTPS请求报SSLException: Certificate for <域名> doesn‘t match any of the subject alternative

    目录 1:HttpClient 的https证书验证请求问题 2: post请求示例 3: get请求示例 1:HttpClient 的https证书验证请求问题 最近用http 推送,用apache ...

  6. java使用httpclient发送POST请求【java基础】

    1.创建请求对象:post或者get HttpPost httpPost = new HttpPost(url); 2. 创建httpclient对象 CloseableHttpClient http ...

  7. 用HttpClient发送HTTPS请求报SSLException: Certificate for <域名> doesn’t match any of the subject alternativ

    用exchage获取Outlook数据服务是因为证书问题导致,现在需要去除证书验证就可以访问了. ExchangeService service = new ExchangeServiceWithHo ...

  8. 对接服务,对方服务器更新SSL证书,服务https的get和post报错。 sun.security.validator.ValidatorException: PKIX path building

    错误信息 javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path build ...

  9. springboot 使用restTemplate 发送https请求 忽略ssl证书

    最近在写接口的时候给对方回推数据,发送https请求的时候遇到这么个报错:javax.net.ssl.SSLHandshakeException: sun.security.validator.Val ...

最新文章

  1. [转]优化Flash性能
  2. AI+Science 是人类两大科研范式的结合,工程化正当时
  3. ***网站必备:(经典语句)
  4. mysql端口测试报错_MySQL报错:2003 - Can't connect to MySQL server on 'localhost' (10038)
  5. 45行代码AC_2017年第八届蓝桥杯C/C++ A组第二题(广搜模板+解题报告)
  6. 步步为营 .NET 设计模式学习笔记 十三、Bridge (桥接模式)
  7. 【机器学习】集成学习之stacking
  8. idea自动导入jar包的快捷键
  9. 为什么说“进程是资源分配的单位、线程是调度的单位”?
  10. LINUX不能恢复式安装
  11. notify和notifyall的区别
  12. IEEE的论文哪里可以下载?
  13. ElasticSearch读流程
  14. java 基础的数组 添加
  15. excel切片器_excel:在透视表中使用切片器高效筛选,升职加薪系列
  16. 区块链的正确应用方式与前景
  17. python小游戏——跑酷小恐龙代码开源
  18. WiFi万能钥匙破解显密码版。
  19. k8s之PV以及PVC
  20. 小学生python游戏编程arcade----灯光示例

热门文章

  1. ai机器人接口TTS配置
  2. Linux基础知识--- 1、centos文件夹介绍
  3. PPTP服务端与客户端 修改默认PPTP默认端口1723
  4. 串口收发指示灯电路----电子工程世界论坛
  5. Winform自定义控件 —— 指示灯
  6. 面试经验:深圳IBM和另一家上市公司
  7. netty中实现双向认证的SSL连接
  8. 3dmax2019【3dsmax2019中文版】官方简体中文适用版
  9. 【数模研赛】“华为杯”第十九届中国研究生数学建模竞赛C题分享——(三)问题一模型建立
  10. oracle mysql is null_MySql,Postgres,Oracle和SQLServer忽略IS NOT NULL过滤...