首页 > 编程语言 >sun.security.validator.ValidatorException: PKIXpath building failed: sun.security.provider,javax.net

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

时间:2023-01-04 14:55:55浏览次数:45  
标签:building return sun X509Certificate security null public conn

报错信息:

javax.net.ssT.SSLHandshakeExceptions.certpath.SunCertPathBuilderException: unable to find valid certification path to reguested target

问题描述:

在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

标签:building,return,sun,X509Certificate,security,null,public,conn
From: https://www.cnblogs.com/huoyl/p/17024772.html

相关文章

  • Uncaught (in promise) Neo4jError: WebSocket connection failure. Due to security
    问题使用​​vue-neo4j​​​连接​​neo4j​​数据库时,控制栏出现以下问题:neo4j版本为4.4.5​​Uncaught(inpromise)Neo4jError:WebSocketconnectionfailure.Dueto......
  • Spring Security 源码分析(四):Spring Social实现微信社交登录
    前言在上一章Spring-Security源码分析三-Spring-Social社交登录过程中,我们已经实现了使用​​SpringSocial​​​+​​Security​​的QQ社交登录。本章我们将实现微信的社......
  • Spring Security-- 验证码功能的实现
    springsecurity4添加验证码​​http://www.itwendao.com/article/detail/165400.html​​​​http://www.itdadao.com/articles/c15a754492p0.html​​ @BeanpublicEmbe......
  • Building a Middle Tier Component using NHibernate and Spring.NET
    ​​http://www.codeproject.com/csharp/SpringPlusHibernate.asp​​​​Downloaddemoproject-22.9Kb​​​​Downloadsource-51.2Kb​​​​Downloadb......
  • 【云原生】K8s PSP 和 securityContext 介绍与使用
    目录一、概述二、PodSecurityPolicy的发展1)以前为什么需要PodSecurityPolicy?2)现在为什么PodSecurityPolicy要消失?三、PSP简单使用1)开启PSP2)示例演示1、没有PSP场景测......
  • Sundial (二)
    相关重要的组件一览Triggers(触发器)相关类保存触发器相关参数,例如起止时间,次数,间隔时间等,其中Sundial支持多种类型触发器多种类型的触发器必须重写GetNextOccurrence方......
  • Sundial(一)
    Sundial源码梳理-v2.5.6代码目录一览通过入口点说明实现IServiceCollection,并返回IServiceCollection(Extensions/ScheduleServiceCollectionExtensions.css53行)......
  • Spring Security开发实践
    SpringSecurity是Spring家族中用于提供认证、授权和攻击防护功能的一套安全框架。它也是Spring应用在安全框架方面的公认标准。SpringSecurity安全框架适合为Sp......
  • 一文搞懂SecurityContext
    一文搞懂SecurityContext1概述​ 首先需要阐明什么是SecurityContext,这是著名框架SpringSecurity中的组件,通过一段时间的研究,我可以很负责的说,在笔者微乎其微的智商水......
  • Android开发 Direct local .aar file dependencies are not supported when building
    问题原因更新了build版本 gradle-7.2后,aar已经不让直接依赖了。在打包apk的时候会出现如下报错信息Directlocal.aarfiledependenciesarenotsupportedwhe......