【解决】Caused by: javax.net.ssl.SSLHandshakeException: PKIX path building failed

问题原因:

在Java8及高版本以上的版本在源应用程序不信任目标应用程序的证书,因为在源应用程序的JVM信任库中找不到该证书或证书链。也就是目标站点启用了HTTPS 而缺少安全证书时出现的异常

解决方案:

我使用的是忽略证书验证

java 复制代码
public class Base64Util {

    public static String getBase64FromUrl(String fileUrl) {
        InputStream inputStream = null;
        byte[] data = null;
        ByteArrayOutputStream swapStream = null;
        HttpsURLConnection conn = null;
        try {
            URL url = new URL(fileUrl);
            //判断当前文件url是否是https
            if (fileUrl.contains("https:")){
                //是https
                //绕过证书
                SSLContext context = createIgnoreVerifySSL();
                conn = (HttpsURLConnection) url.openConnection();
                conn.setSSLSocketFactory(context.getSocketFactory());
                inputStream = conn.getInputStream();
            }else {
                //当前链接是http
                inputStream =  url.openConnection().getInputStream();
            }

            swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[100];
            int rc = 0;
            while ((rc = inputStream.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            data = swapStream.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(swapStream);
        }
        return new String(Base64.encodeBase64(data));
    }

    //绕过SSL、TLS证书
    public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext sc = SSLContext.getInstance("TLS");
        // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
        X509TrustManager trustManager = new X509TrustManager() {
            @Override
            public void checkClientTrusted(
                    java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                    String paramString) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(
                    java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                    String paramString) throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        sc.init(null, new TrustManager[]{trustManager}, null);
        return sc;
    }
}
相关推荐
@atweiwei8 小时前
LangChainRust 0.33.0 发布:让 Agent 应用更容易运行、观察和评测
服务器·前端·javascript
工业一体机老司机8 小时前
rsync+inotify 收集车间工位机日志的完整配置
服务器·数据库·postgresql
西柚小萌新8 小时前
【人工智能:LangGraph】--3.控制流(Edge)
java·服务器·数据库
SHENGWEI胜为11 小时前
胜为CZDF-624六类24口免打配线架怎么安装?机房布线实操解析
服务器·网络·综合布线·rj45接口·胜为光通信·六类配线架
图映 图片处理11 小时前
浏览器本地压缩图片安全吗:基于网络请求与文件传输的验证方法
服务器·网络·安全
DeskUI~~13 小时前
8.4M,我把 Linux 变成了一块“磁盘”
linux·运维·服务器
无足鸟ICT13 小时前
【RHCA+】_proc_pid_environ
linux·运维·服务器
智购科技无人售货机工厂店13 小时前
自动售货机振动与运输可靠性测试实战:跌落、振动与包装~YH
java·大数据·服务器·人工智能·科技
wacsii14 小时前
【Linux操作系统】从零手写 mystdio:静态库、动态库制作与第三方链接踩坑
linux·服务器
小张成长计划..14 小时前
【Linux】44:五种IO模型与非阻塞IO
java·服务器·数据库