本文介绍了sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很沮丧要解决以下错误.我正在使用 RestTemplate API 运行 junit 测试用例,该 API 调用在 8443

上运行的 POST webservice

org.springframework.web.client.ResourceAccessException:对 **"https://localhost:8443/google-rest/student?**":sun.security.validator POST 请求时出现 I/O 错误.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效认证路径;嵌套异常是 javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效认证路径在 org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:567)在 org.springframework.web.client.RestTemplate.execute(RestTemplate.java:512)在 org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:454)在 com.altruista.mp.rest.sso.SSOTest.testSSOLogin(SSOTest.java:58)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在 java.lang.reflect.Method.invoke(Method.java:497)在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)在 org.junit.runners.ParentRunner.run(ParentRunner.java:309)在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)引起:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效认证路径在 sun.security.ssl.Alerts.getSSLException(Alerts.java:192)在 sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1937)在 sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)在 sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)在 sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1478)在 sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212)在 sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)在 sun.security.ssl.Handshaker.process_record(Handshaker.java:914)在 sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1050)在 sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)在 sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391)在 sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375)在 sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)在 sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)在 sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)在 org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:75)在 org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)在 org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)在 org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:551)... 26 更多引起:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效认证路径在 sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)在 sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)在 sun.security.validator.Validator.validate(Validator.java:260)在 sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)在 sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)在 sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)在 sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1460)……还有 40 个引起:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效认证路径在 sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145)在 sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)在 java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)在 sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)... 46 更多

根据其他链接,我做了以下操作:

keytool -import -alias student_rsa -file D:\student_rsa.der -keystoreC:\Program Files\Java\jdk1.8.0_45\jre\lib\security\cacerts

我看到上面的命令执行出现以下错误:-

非法选项:Files\Java\jdk1.8.0_45\jre\lib\security\cacertskeytool -importcert [选项]...导入证书或证书链选项:-noprompt 不提示-trustcacerts 信任来自 cacerts 的证书- 通过保护机制保护密码-alias 要处理的条目的别名-file 输入文件名-keypass <arg>钥匙密码-keystore <keystore>密钥库名称-storepass 密钥库密码-storetype <storetype>密钥库类型-providername提供者名称-providerclass 提供者类名-providerarg 提供者参数-providerpath 提供者类路径-v 详细输出对所有可用命令使用keytool -help"
解决方案

您需要在运行单元测试的 JVM 服务器上安装证书.为此,您需要先下载网络服务证书.

您可以从任何浏览器下载它(右键单击搜索栏中的证书图标,然后在详细信息"选项卡中,单击复制到文件")

然后运行keytool命令(注意你需要添加包含下载的证书路径的-file参数)

keytool -import -alias student_rsa -keystore C:\Program Files\Java\jdk1.8.0_45\jre\lib\security\cacerts -file YourCertificate.cer

您还需要检查 JUnit 是否使用相同的 jdk 安装路径,因为它通常使用 jre 安装路径

这个链接帮助我解决了这个问题.>

希望这对你有用

问候,兰德尔·罗萨莱斯

I am really frustrated to solve below error. I am running junit test cases using RestTemplate API which calls POST webservice which runns on 8443

org.springframework.web.client.ResourceAccessException: I/O error on POST request for **"https://localhost:8443/google-rest/student?**":sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is 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 org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:567)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:512)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:454)
    at com.altruista.mp.rest.sso.SSOTest.testSSOLogin(SSOTest.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: 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:1937)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1478)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1050)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
    at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:75)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:551)
    ... 26 more
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:387)
    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:324)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1460)
    ... 40 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
    ... 46 more

As per other links, I did following:

keytool -import -alias student_rsa -file D:\student_rsa.der -keystore
C:\Program Files\Java\jdk1.8.0_45\jre\lib\security\cacerts

I see following error comes for above command execution:-

Illegal option:  Files\Java\jdk1.8.0_45\jre\lib\security\cacerts
keytool -importcert [OPTION]...

Imports a certificate or a certificate chain

Options:

 -noprompt                       do not prompt
 -trustcacerts                   trust certificates from cacerts
 -protected                      password through protected mechanism
 -alias <alias>                  alias name of the entry to process
 -file <filename>                input file name
 -keypass <arg>                  key password
 -keystore <keystore>            keystore name
 -storepass <arg>                keystore password
 -storetype <storetype>          keystore type
 -providername <providername>    provider name
 -providerclass <providerclass>  provider class name
 -providerarg <arg>              provider argument
 -providerpath <pathlist>        provider classpath
 -v                              verbose output

Use "keytool -help" for all available commands
解决方案

You need to install a certificate on the JVM server running the unit tests. In order to do this, you need to download the web service certificate first.

You can download it from any browser (right click on the certificate icon in the search bar and in the Details tab, click Copy to File)

Then run the keytool command (notice you need to add -file parameter that contains the downloaded certificate path)

keytool -import -alias student_rsa -keystore C:\Program Files\Java\jdk1.8.0_45\jre\lib\security\cacerts -file YourCertificate.cer

You also need to check if JUnit is using the same jdk installation path since it usually used the jre installation path

This link helped me to solve this issue.

I hope this will work for you

Regards,Randall Rosales

这篇关于sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 22:07
查看更多