Java调用https服务报错unable to find valid certification path to requested target的解决方法

Wesley13
• 阅读 879

我们网站要进行https改造,配置上购买的SSL证书后,浏览器访问正常,但是写了个java代码用httpcomponents调用https rest接口时报错:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)

at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1904)

at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:279)

at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:273)

at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1446)

at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:209)

at sun.security.ssl.Handshaker.processLoop(Handshaker.java:901)

at sun.security.ssl.Handshaker.process_record(Handshaker.java:837)

at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1023)

at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)

at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359)

at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343)

at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)

at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)

at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)

at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)

at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)

at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)

at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)

at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)

at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)

at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)

at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)

at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)

at com.duiba.activity.cmsweb.controller.DappConfigCtrl.main(DappConfigCtrl.java:1248)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)

at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)

at sun.security.validator.Validator.validate(Validator.java:260)

at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)

at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)

at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)

at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1428)

... 25 more

Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)

at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)

at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)

... 31 more

网上查了一堆资料,要么说要把网站证书放到某个目录下,要么要改代码,没有我想要的,因为不可能让其他开发者去做这些事情。

后来了解到证书链这回事,才知道如何解决这个问题。有关证书链可以读这里:http://blog.sina.com.cn/s/blog\_53ed87c10102vn8b.html

此问题产生的原因是因为我们运维配置证书时只使用了签发的证书,java客户端无法找到可信任的上级证书,所以报错。解决方法也很简单,把中级证书、根证书附加到签发证书后面就可以了,具体方法参考这里:https://yq.aliyun.com/articles/26569

点赞
收藏
评论区
推荐文章
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Easter79 Easter79
3年前
spring数据源DriverManagerDataSource和BasicDataSource
    说起来没什么好说的东西,Java后台的配置还是照样的配,服务一直调用着正欢的时候发现跑个1000条记录tomcat就会有报错,大致错误如下:org.springframework.transaction.CannotCreateTransactionException:CouldnotopenHibernateSession
待兔 待兔
4年前
Java多态实现原理
Java多态概述多态是面向对象编程语言的重要特性,它允许基类的指针或引用指向派生类的对象,而在具体访问时实现方法的动态绑定。Java对于方法调用动态绑定的实现主要依赖于方法表,但通过类引用调用(invokevirtual)和接口引用调用(invokeinterface)的实现则有所不同。类引用调用的大致过程为:Java编译器将Java源代码编译成c
Wesley13 Wesley13
3年前
Failed to get nested archive for entry BOOT
环境:springbootkafkaES。。。。。。maven3.6jdk8问题是jar包启动失败报错,错误如下:Exceptioninthread"main"java.lang.IllegalStateException:FailedtogetnestedarchiveforentryBO
Stella981 Stella981
3年前
SSL连接报错:javax.net.ssl.SSLHandshakeException
在使用SocketSSL双向连接时,客户端可以连接上服务端,但传输数据时报错javax.net.ssl.SSLHandshakeException:nullcertchain原因是使用的KeyStore文件不对首先创建服务器端私有密钥和公共密钥1,keytoolgenkeyaliasserverkeykeystore
Easter79 Easter79
3年前
SSL连接报错:javax.net.ssl.SSLHandshakeException
在使用SocketSSL双向连接时,客户端可以连接上服务端,但传输数据时报错javax.net.ssl.SSLHandshakeException:nullcertchain原因是使用的KeyStore文件不对首先创建服务器端私有密钥和公共密钥1,keytoolgenkeyaliasserverkeykeystore
Stella981 Stella981
3年前
Native
   privatenativevoidA();//不报错只有方法定义,没有方法实现,却不报错NativeInterface本地接口 本地接口的作用是融合不同的编程语言为Java所用,它的初衷是融合C/C程序,Java诞生的时候是C/C横行的时候,要想立足,必须有调用C/C程序,于是就在内存中专门开
Wesley13 Wesley13
3年前
Java8 Optional && Guava Optional
Java8OptionalbooleanisPresent():与obj!null()一样;调用get()前要调用isPresent()检查,不然会报错Optional的三种构造方式:Optional.of(obj):obj不能为null,不然报npeOptional.ofNullable(
Stella981 Stella981
3年前
Spring Cloud微服务架构从入门到会用(三)—服务间调用Feign
微服务最重要的一个功能是服务间调用,各个服务互相依赖。比如电商系统有订单服务,有库存服务。在我们购买一件商品的时候,需要生成订单和减库存。这里我们就要用到服务间调用Feign。Feign是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求,而不用像Java中通过封装HTTP请求报文的方式直接调用。接下来我们新建两个modu
Wesley13 Wesley13
3年前
unable to find valid certification path to request
解决PKIX问题:unabletofindvalidcertificationpathtorequestedtarget话说前几天在测试服务器上遇到了这么个异常javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException