Java在发送Https请求的时候,不可避免的会发生SSL证书认证失败

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

错误原因是因为发送Https请求的时候 SSL证书认证失败

事发现场:在使用HttpClient调用支付宝的Https接口时发生的错误

解决方法:创建一个HttpSSL类,代码如下

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager; public class HttpSSL {
private static void trustAllHttpsCertificates() throws Exception {
javax.net.ssl.TrustManager[] trustAllCerts = new 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());
} public static void solveSSL() throws Exception{
trustAllHttpsCertificates();
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) { return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
} 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;
}
}
}

使用方式,在发送Https请求之前,调用该类的 solveSSL()方法忽略证书认证问题

OK,基本能解决SSL认证失败的问题了!

 

本人唯一QQ:1132017151

欢迎来讨论Java相关问题!

ValidatorException-异常的更多相关文章

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

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

  2. 我是如何解决java.security.cert.CertPathValidatorException异常的

    目录 问题来了 问题分析 解决问题 重新安装服务器端证书 日志带来曙光 刨根到底 总结 附录 tomcat的SSL配置 服务器端证书配置 Keytool命令常用参数 问题来了 昨天,我还在我的工位上愉 ...

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

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

  4. Jsoup访问https网址异常SSLHandshakeException(已解决)

    爬取网页遇到的目标站点证书不合法问题. 使用jsoup爬取解析网页时,出现了如下的异常情况. javax.net.ssl.SSLHandshakeException: sun.security.val ...

  5. 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 ...

  6. 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: ...

  7. alias导致virtualenv异常的分析和解法

    title: alias导致virtualenv异常的分析和解法 toc: true comments: true date: 2016-06-27 23:40:56 tags: [OS X, ZSH ...

  8. ASP.NET Core应用的错误处理[2]:DeveloperExceptionPageMiddleware中间件如何呈现“开发者异常页面”

    在<ASP.NET Core应用的错误处理[1]:三种呈现错误页面的方式>中,我们通过几个简单的实例演示了如何呈现一个错误页面,这些错误页面的呈现分别由三个对应的中间件来完成,接下来我们将 ...

  9. 记一次tomcat线程创建异常调优:unable to create new native thread

    测试在进行一次性能测试的时候发现并发300个请求时出现了下面的异常: HTTP Status 500 - Handler processing failed; nested exception is ...

  10. 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法

    在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常.   意思是出现了死循环,也就是Model之间有循环包含关系: ...

随机推荐

  1. C语言知识点汇集

    int main() {// int num; int value; = int num,value; '''同时定义多个变量的方法 但是切记只能是同种类型的''' 都是int 或double等其他类 ...

  2. Java内部类使用场景和作用

    一.Java内部类的分类 Java内部类一般包括四种:成员内部类.局部内部类.匿名内部类和静态内部类 大多数业务需求,不使用内部类都可以解决,那为什么Java还要设计内部类呢. 二.内部类的使用场景 ...

  3. 深度长文整理-Redis进阶

    目录 一.基础 二.为什么Redis是单线程的? 三.为什么单线程这么快? 四.select.poll.epoll 五.Redis的事物 六.Redis的监控 七.Redis的配置文件 八.Redis ...

  4. php第七天-文件处理系统

    0x01 文件系统概述 1.1文件类型 在程序运行时,程序本身和数据一般都存在内存中,当程序运行结束后,存放在内存中的数据被释放. 如果需要长期保存程序运行所需的原始数据,或程序运行产生的结果,就必须 ...

  5. 使用maven纯注解集成ssm

    1.配置springMVC框架 第一步:导入包依赖 <!--配置springMVC--> <dependency> <groupId>javax.servlet.j ...

  6. 吴恩达Machine Learning学习笔记(一)

    机器学习的定义 A computer program is said to learn from experience E with respect to some class of tasks T ...

  7. Akka Netty 比较

    从Akka出现背景来说,它是基于Actor的RPC通信系统,它的核心概念也是Message,它是基于协程的,性能不容置疑:基于scala的偏函数,易用性也没有话说,但是它毕竟只是RPC通信,无法适用大 ...

  8. 和低效 IO 说再见,回头补一波 Java 7 的 NIO.2 特性

    其实在这之前已经写过一篇关于 Java 7 的新特性文章了,那篇文章主要介绍了 Java 7 的资源自动关闭.Switch String 实现原理.异常捕获 try-catch.新的二进制书写方式等, ...

  9. spring ioc 源码分析(三)--finishBeanFactoryInitialization(beanFactory)

    之前的博客专门分析了一个类到BeanDefinition的创建过程,现在分析BeanDefinition 到一个bean的创建过程:从refresh() 方法的---->finishBeanFa ...

  10. LVS搭建

    LVS集群搭建 NAT架构图 1.在RS188,RS189操作 #安装httpd服务[root@t1 ~]# yum install -y httpd[root@t1 ~]# echo "I ...