sun.security.validator.ValidatorException: PKIXpath building failed: sun.security.provider,javax.net

这篇具有很好参考价值的文章主要介绍了sun.security.validator.ValidatorException: PKIXpath building failed: sun.security.provider,javax.net。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

报错信息:

sun.security.validator.ValidatorException: PKIXpath building failed: 
sun.security.provider,javax.net.ssT.SSLHandshakeExceptions.certpath.SunCertPathBuilderException: unable to find valid certification path to reguested target

sun.security.validator.ValidatorException: PKIXpath building failed: sun.security.provider,javax.net,https,java

问题描述:

在java代码中调用其他项目接口,发起的是https请求。报错信息说找不到有效证书路径。

问题解决:

信任所有SSL证书

1、新建一个SslUtil类

package com.asiainfo.strategy.cloud.base.utils;

import javax.net.ssl.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * @Author huoyl
 * @create 2023/1/3 14:45
 */
public class SslUtil {
    private static void trustAllHttpsCertificates() throws Exception {
        TrustManager[] trustAllCerts = new TrustManager[1];
        TrustManager tm = new miTM();
        trustAllCerts[0] = tm;
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    }
    static class miTM implements TrustManager, X509TrustManager {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public boolean isServerTrusted(X509Certificate[] certs) {
            return true;
        }
        public boolean isClientTrusted(X509Certificate[] certs) {
            return true;
        }
        public void checkServerTrusted(X509Certificate[] certs, String authType)
                throws CertificateException {
            return;
        }
        public void checkClientTrusted(X509Certificate[] certs, String authType)
                throws CertificateException {
            return;
        }
    }
    /**
     * 忽略HTTPS请求的SSL证书,必须在openConnection之前调用
     * @throws Exception
     */
    public static void ignoreSsl() throws Exception{
        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
                return true;
            }
        };
        trustAllHttpsCertificates();
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
    }
}

2、在HttpUtil工具类中修改代码


        InputStream inputStream = null;
        OutputStream outStream = null;
        HttpURLConnection conn = null;
        try {
        	byte[] entity = jsonObject.toJSONString().getBytes();
		//信任所有SSL证书
		URL url = new URL(path);
		if("https".equalsIgnoreCase(url.getProtocol())){
			SslUtil.ignoreSsl();
		}
		conn = (HttpURLConnection) url.openConnection();
//		conn = (HttpURLConnection) new URL (path).openConnection ();
            conn.setConnectTimeout (5000);// 设置超时
            conn.setRequestMethod ("POST");
            // 允许对外输出数据
            conn.setDoOutput (true);
            ...
        } catch (Exception e) {
            e.printStackTrace ();
            logger.info("http调用发生异常,错误信息:{}", e.getMessage());
        } finally {
        	if (outStream != null) {
        		outStream.close();
            }
            if (conn != null) {
                conn.disconnect ();
            }
        }

忽略HTTPS请求的SSL证书代码,必须在openConnection之前调用

解决方案参考文章https://developer.aliyun.com/article/812846文章来源地址https://www.toymoban.com/news/detail-716705.html

到了这里,关于sun.security.validator.ValidatorException: PKIXpath building failed: sun.security.provider,javax.net的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包