前段时间开发的一个需求,需要通过图片URL获取图片的base64编码,测试的时候使用的是百度图片的url,测试没有问题,但是发布后测试时报如下错:

  1. javax.net.ssl.sslhandshakeException:sun.security.validator.validatorException:PKIX path buildind failed

报错代码:

  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3.  
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.InputStream;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. import java.util.Base64;
  10.  
  11. public class Base64Util {
  12.  
  13. private static final Logger logger = LoggerFactory.getLogger(Base64Util.class);
  14.  
  15. public static String getBase64ByUrl(String urlPath){
  16. ByteArrayOutputStream data = new ByteArrayOutputStream();
  17. try {
  18. URL url = new URL(urlPath);
  19. byte[] by = new byte[1024];
  20. URLConnection urlConnection = url.openConnection();
  21. HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
  22. httpURLConnection.setConnectTimeout(1000*5);
  23. httpURLConnection.connect();
  24. InputStream inputStream = httpURLConnection.getInputStream();
  25. int len = -1;
  26. while ( (len = inputStream.read(by)) !=-1){
  27. data.write(by,0,len);
  28. }
  29. inputStream.close();
  30. } catch (Exception e) {
  31. logger.error("获取图片base64出错:" + e + ",图片url为:" + urlPath);
  32. }
  33. return Base64.getMimeEncoder().encodeToString(data.toByteArray());
  34. }
  35. }

检查发现是jdk的证书库里并没有将该站点的证书作为受信任的安全证书,只要在打开链接的时候设置忽略证书检查就可以解决,更新代码如下:

  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3.  
  4. import javax.net.ssl.HostnameVerifier;
  5. import javax.net.ssl.HttpsURLConnection;
  6. import javax.net.ssl.SSLSession;
  7. import java.io.ByteArrayOutputStream;
  8. import java.io.InputStream;
  9. import java.net.HttpURLConnection;
  10. import java.net.URL;
  11. import java.net.URLConnection;
  12. import java.util.Base64;
  13.  
  14. public class Base64Util {
  15.  
  16. private static final Logger logger = LoggerFactory.getLogger(Base64Util.class);
  17.  
  18. public static String getBase64ByUrl(String urlPath){
  19. ByteArrayOutputStream data = new ByteArrayOutputStream();
  20. try {
  21. HostnameVerifier hv = new HostnameVerifier() {
  22. public boolean verify(String urlHostName, SSLSession session) {
  23. System.out.println("Warning: URL Host: " + urlHostName + " vs. "
  24. + session.getPeerHost());
  25. return true;
  26. }
  27. };
  28. URL url = new URL(urlPath);
  29. byte[] by = new byte[1024];
  30. //忽略证书信任
  31. trustAllHttpsCertificates();
  32. HttpsURLConnection.setDefaultHostnameVerifier(hv);
  33. URLConnection urlConnection = url.openConnection();
  34. HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
  35. httpURLConnection.setConnectTimeout(1000*5);
  36. httpURLConnection.connect();
  37. InputStream inputStream = httpURLConnection.getInputStream();
  38. int len = -1;
  39. while ( (len = inputStream.read(by)) !=-1){
  40. data.write(by,0,len);
  41. }
  42. inputStream.close();
  43. } catch (Exception e) {
  44. logger.error("获取图片base64出错:" + e + ",图片url为:" + urlPath);
  45. }
  46. return Base64.getMimeEncoder().encodeToString(data.toByteArray());
  47. }
  48.  
  49. private static void trustAllHttpsCertificates() throws Exception {
  50. javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
  51. javax.net.ssl.TrustManager tm = new miTM();
  52. trustAllCerts[0] = tm;
  53. javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
  54. .getInstance("SSL");
  55. sc.init(null, trustAllCerts, null);
  56. javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
  57. .getSocketFactory());
  58. }
  59.  
  60. static class miTM implements javax.net.ssl.TrustManager,
  61. javax.net.ssl.X509TrustManager {
  62. public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  63. return null;
  64. }
  65.  
  66. public boolean isServerTrusted(
  67. java.security.cert.X509Certificate[] certs) {
  68. return true;
  69. }
  70.  
  71. public boolean isClientTrusted(
  72. java.security.cert.X509Certificate[] certs) {
  73. return true;
  74. }
  75.  
  76. public void checkServerTrusted(
  77. java.security.cert.X509Certificate[] certs, String authType)
  78. throws java.security.cert.CertificateException {
  79. return;
  80. }
  81.  
  82. public void checkClientTrusted(
  83. java.security.cert.X509Certificate[] certs, String authType)
  84. throws java.security.cert.CertificateException {
  85. return;
  86. }
  87. }
  88. }

在URLConnection urlConnection = url.openConnection();之前忽略证书信任,问题解决。

javax.net.ssl.sslhandshakeException:sun.security.validator.validatorException:PKIX path buildind failed的更多相关文章

  1. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed

    1.使用HttpClient4.3 调用https出现如下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Validat ...

  2. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certificatio

    场景:Java调用PHP接口,代码部署在服务器上后,调用报错,显示PHP服务器那边证书我这边服务器不信任(我猜的). 异常信息: 2019-08-06 14:00:09,102 [http-nio-4 ...

  3. 异常信息:javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed

    上周五遇到一个问题,工程本地编译运行正常,打包本地tomcat运行也正常.部署到测试环境报错: 2017-05-05 09:38:11.645 ERROR [HttpPoolClientsUtil.j ...

  4. javax.net.ssl.SSLHandshakeException sun.security.validator.ValidatorException PK

    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building f ...

  5. 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法

    抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...

  6. 解决 sun.security.validator.ValidatorException: PKIX path building failed

    今天用java HttpClients写爬虫在访问某Https站点报如下错误: sun.security.validator.ValidatorException: PKIX path buildin ...

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

    还是记录使用 maven 时遇到的问题. 一.maven报错 maven package 进行打包时出现了以下报错: Non-resolvable parent POM for com.wpbxin: ...

  8. mvn 编译报错mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targ

    mavn 编译报错: mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.p ...

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

    httpclient-4.5.jar 定时发送http包,忽然有一天报错,http证书变更引起的. 之前的代码 try { CloseableHttpClient httpClient = build ...

随机推荐

  1. IC应届生40万白菜价!从业多年的资深专家手把手指导你如何选择offer!

    这是IC男奋斗史的第1篇原创 关注公众号[IC男奋斗史],让我们一起撸起袖子加油干! 芯片行业2021年的秋招市场又是风起云涌.高手过招,继OPPO给应届生开出40万白菜价offer之后,平头哥又被爆 ...

  2. [旧][Android] 命名规范和编码规范

    备注 原发表于2016.05.07,资料已过时,仅作备份,谨慎参考 前言 本文适用范围:已参加项目开发的人 写这篇文章的目的是为方便地对代码进行管理,让整个团队的代码规范化.这里的部分规定可能和你在其 ...

  3. 还在用excel做分析?你已经out了!

    Excel 是个很有趣的工具,不管你是不是数据分析领域的打工人,都一定听过它的名字,甚至在全球拥有大量虔诚的粉丝.Excel这个名字其实源自英语中的" Excellence "一词 ...

  4. Android studio第一个程序HelloWorld

    今天主要跟着视频设计了第一个安卓项目,了解了大改的目录结构 每天会学习线性布局和相对布局

  5. Spring AOP之3w的配置

    1.3w是什么? ■ what.where.when 或者 what.when.where 2.what.where.when[通用] (1)what:增强器-bean[配置一个bean对象] (2) ...

  6. Ubutun:镜像网站

    中科大镜像站(地址:安徽):http://mirrors.ustc.edu.cn清华大学镜像站:http://mirrors.tuna.tsinghua.edu.cn浙江大学镜像站:http://mi ...

  7. LeetCode-006-Z 字形变换

    Z 字形变换 题目描述:将一个给定字符串 s 根据给定的行数 numRows ,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "PAYPALISHIRING" 行数为 ...

  8. git合并分支代码的方法

    1.先提交本地代码,防止被拉取其他分支的代码污染(self为自己的分支 other为想要拉取的分支) git add . git commit -m '备注信息' git push origin se ...

  9. 【AI】AI学习方向

    df

  10. Sequelize关联查询,where影响全局的解决

    需求背景 系统字段关联公司字段表,通过查询所有的系统字段然后关联查询指定的公司对应的字段. 问题代码 const sysField = ctx.model.SysDictionary; const c ...