它看起来像一个标准问题,但我无法在任何地方找到明确的方向.
我有java代码尝试连接到可能具有自签名(或过期)证书的服务器.代码报告以下错误:
[HttpMethodDirector] I/O exception (javax.net.ssl.SSLHandshakeException) caught
when processing request: sun.security.validator.ValidatorException: PKIX path
building failed: sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target
Run Code Online (Sandbox Code Playgroud)
据我了解,我必须使用keytool并告诉java允许这种连接是可以的.
解决此问题的所有说明都假设我完全熟练使用keytool,例如
为服务器生成私钥并将其导入密钥库
有没有人可以发布详细说明?
我正在运行unix,所以bash脚本最好.
不确定它是否重要,但代码是在jboss中执行的.
假设我们需要信任自签名的SSL证书.例如,让我们使用https://self-signed.badssl.com/.
由于签名者不是"适当的"权限,Java不信任它并拒绝连接到该服务器.然而,之后
$ cd $JAVA_HOME/jre/lib/security
$ keytool -import -trustcacerts -alias ... -file ... -keystore cacerts
Run Code Online (Sandbox Code Playgroud)
并重新启动应用程序,以下代码有效:
new URL ("https://self-signed.badssl.com/").openConnection ().getResponseCode ()
Run Code Online (Sandbox Code Playgroud)
并返回200(OK),不抛出异常.即打开HTTPS连接的基本Java方法现在可以使用,因为证书现在是可信的.
但是,这对javax.ws.rs客户端没有任何明显的影响(至少在Resteasy中实现),我仍然得到一个例外:
javax.ws.rs.ProcessingException: Unable to invoke request
at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:287)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:407)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.method(ClientInvocationBuilder.java:273)
[...]
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:1949)
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:1506)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
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:1062)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at …Run Code Online (Sandbox Code Playgroud)