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

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

报错代码:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64; public class Base64Util { private static final Logger logger = LoggerFactory.getLogger(Base64Util.class); public static String getBase64ByUrl(String urlPath){
ByteArrayOutputStream data = new ByteArrayOutputStream();
try {
URL url = new URL(urlPath);
byte[] by = new byte[1024];
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setConnectTimeout(1000*5);
httpURLConnection.connect();
InputStream inputStream = httpURLConnection.getInputStream();
int len = -1;
while ( (len = inputStream.read(by)) !=-1){
data.write(by,0,len);
}
inputStream.close();
} catch (Exception e) {
logger.error("获取图片base64出错:" + e + ",图片url为:" + urlPath);
}
return Base64.getMimeEncoder().encodeToString(data.toByteArray());
}
}

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64; public class Base64Util { private static final Logger logger = LoggerFactory.getLogger(Base64Util.class); public static String getBase64ByUrl(String urlPath){
ByteArrayOutputStream data = new ByteArrayOutputStream();
try {
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: " + urlHostName + " vs. "
+ session.getPeerHost());
return true;
}
};
URL url = new URL(urlPath);
byte[] by = new byte[1024];
//忽略证书信任
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setConnectTimeout(1000*5);
httpURLConnection.connect();
InputStream inputStream = httpURLConnection.getInputStream();
int len = -1;
while ( (len = inputStream.read(by)) !=-1){
data.write(by,0,len);
}
inputStream.close();
} catch (Exception e) {
logger.error("获取图片base64出错:" + e + ",图片url为:" + urlPath);
}
return Base64.getMimeEncoder().encodeToString(data.toByteArray());
} private static void trustAllHttpsCertificates() throws Exception {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
.getSocketFactory());
} static class miTM implements javax.net.ssl.TrustManager,
javax.net.ssl.X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
} public boolean isServerTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
} public boolean isClientTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
} public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
} public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
}
}

在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. demo_2_27

    #define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>#include <string.h> int count_bit_one ...

  2. IDEA tomcat启动报错----Artifact is being deployed, please wait...解决

    今天学习遇到了这个错误,记录下自己遇到的错误和解决方法! 这个报错的意思是: Artifact 正在部署中,请稍候- 实际上有可能就是jar包没有导进去.检查项目打包情况:file-->Proj ...

  3. 案例六:shell脚本监控httpd服务80端口状态

    这里是举例监控httpd服务端口状态,根据端口判断服务器是否启动,如果没有启动则脚本自动拉起服务,如果服务正在运行则退出脚本程序:如果换成别的服务端口也可以,但是脚本程序需要做调整. #!/bin/b ...

  4. Docker学习笔记(详细)

    目录 01 介绍 02 Docker安装 03 Docker常用命令 04 Docker镜像 05 Docker容器数据卷 06 Dockerfile解析 Dockerfile构建过程解析 Docke ...

  5. 通过xmanager连接linux远程主机桌面

    转至:https://blog.csdn.net/kadwf123/article/details/79564293 1.效果图: 远程linux桌面版主机,此处是虚拟机: 使用xmanager xb ...

  6. 痞子衡嵌入式:揭秘i.MXRTxxx系列上串行NOR Flash双程序可交替启动设计

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT500/600上串行NOR Flash双程序可交替启动设计. 在上一篇文章 <i.MXRT1170上串行NOR Fla ...

  7. 7.Flink实时项目之独立访客开发

    1.架构说明 在上6节当中,我们已经完成了从ods层到dwd层的转换,包括日志数据和业务数据,下面我们开始做dwm层的任务. DWM 层主要服务 DWS,因为部分需求直接从 DWD 层到DWS 层中间 ...

  8. 《破碎的残阳,我们逆光》连载小说- HashMap剖析

    破碎的残阳,我们逆光[连载小说]- HashMap剖析 "行到水穷处,坐看云起时"        前言: 偶尔翻阅了自己当时高中时代写的日志,发现了几篇自己多年未打开的自写小说草本 ...

  9. PyTorch深度学习实践——处理多维特征的输入

    处理多维特征的输入 课程来源:PyTorch深度学习实践--河北工业大学 <PyTorch深度学习实践>完结合集_哔哩哔哩_bilibili 这一讲介绍输入为多维数据时的分类. 一个数据集 ...

  10. Qt:QJsonDocument以及与QJsonArray、QJsonObject、QJsonValue的关联

    0.说明 QJsonDocument类提供了read/write JSON文档的方法. 用QJsonDocument::fromJson()方法,可以从将一个JSON文件(或者QByteArray数据 ...