有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

https告诉java客户端代码接受自签名证书

我想连接到一个https服务器,它有一个self signed certificate,而不修改我的客户机代码

我知道这个问题已经被问过很多次了,但我没能让它起作用。以下是我所做的:

  1. 在Firefox中打开https服务器

  2. 单击url信息,然后单击更多信息,然后查看证书,然后单击详细信息,然后单击导出

  3. 选择默认的导出类型设置(X.509 Certificate (PEM),另存为certificate.crt),并将其保存到磁盘

  4. 打开命令提示符,转到客户机代码使用的java_home文件夹,在我的例子中是c:\jdk-7u55-windows-x64\jre\bin

  5. 输入以下命令:

    keytool -import -v -trustcacerts
                -alias server-alias -file C:\Downloads\certificate.crt
                -keystore cacerts.jks -keypass changeit
                -storepass changeit
    

我也试过:

   keytool -import -v -trustcacerts
                    -alias server-alias2 -file C:\Downloads\certificate.crt
                    -keystore keystore.jks
  1. 重新启动客户端应用程序

在所有这些之后,我仍然得到一个SSLHandshakeException(无法找到到请求目标的有效认证路径)。 日志中的完全异常:

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

我不知道我做错了什么,还需要做什么,或者如何进一步调试


共 (1) 个答案

  1. # 1 楼答案

    指定所需cacerts.jks的绝对路径应该会有所帮助。当前使用keytool只会产生新的结果。jks到您当前的目录(我想)是c:\jdk-7u55-windows-x64\jre\bin。默认的cacerts位置通常在JAVA_HOME/jre/lib/security/cacerts。或者你可以使用 -Djavax.net.ssl.trustStore=<path to custom store> -Djavax.net.ssl.trustStorePassword=<custom store_passwd>作为应用程序的起始参数More info