默认情况下,如何将自签名证书正确导入到所有 Java 应用程序都可用的 Java 密钥库中? [英] How to properly import a selfsigned certificate into Java keystore that is available to all Java applications by default?

查看:27
本文介绍了默认情况下,如何将自签名证书正确导入到所有 Java 应用程序都可用的 Java 密钥库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实想将自签名证书导入 Java,以便任何尝试建立 SSL 连接的 Java 应用程序都将信任此证书.

I do want to import a self signed certificate into Java so any Java application that will try to establish a SSL connection will trust this certificate.

到目前为止,我设法将其导入

So far, I managed to import it in

keytool -import -trustcacerts -noprompt -storepass changeit -alias $REMHOST -file $REMHOST.pem
keytool -import -trustcacerts -noprompt -keystore cacerts -storepass changeit -alias $REMHOST -file $REMHOST.pem

仍然,当我尝试运行 HTTPSClient.class 我仍然得到:

Still, when I try to run HTTPSClient.class I still get:

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

推荐答案

On Windows 最简单的方法是使用程序 门窗.

On Windows the easiest way is to use the program portecle.

  1. 下载并安装 portecle.
  2. 首先确保 100% 确定您知道正在使用哪个 JRE 或 JDK 来运行您的程序.在 64 位 Windows 7 上,可能有很多 JRE.Process Explorer 可以帮助您解决这个问题,或者您可以使用:System.out.println(System.getProperty("java.home"));
  3. 将文件 JAVA_HOMElibsecuritycacerts 复制到另一个文件夹.
  4. 在 Portecle 中点击文件 > 打开密钥库文件
  5. 选择 cacerts 文件
  6. 输入密码:changeit
  7. 点击工具 > 导入可信证书
  8. 浏览文件 mycertificate.pem
  9. 点击导入
  10. 单击确定"以获取有关信任路径的警告.
  11. 在显示有关证书的详细信息时单击确定".
  12. 单击是"以接受证书为受信任的证书.
  13. 当它要求输入别名时,单击确定",并在显示已导入证书时再次单击确定".
  14. 点击保存.不要忘记这一点,否则更改将被丢弃.
  15. 将文件 cacerts 复制回您找到它的位置.

在 Linux 上:

您可以像这样从已经使用它的网络服务器下载 SSL 证书:

You can download the SSL certificate from a web server that is already using it like this:

$ echo -n | openssl s_client -connect www.example.com:443 | 
   sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /tmp/examplecert.crt

可选择验证证书信息:

$ openssl x509 -in /tmp/examplecert.crt -text

将证书导入 Java cacerts 密钥库:

Import the certificate into the Java cacerts keystore:

$ keytool -import -trustcacerts -keystore /opt/java/jre/lib/security/cacerts 
   -storepass changeit -noprompt -alias mycert -file /tmp/examplecert.crt

这篇关于默认情况下,如何将自签名证书正确导入到所有 Java 应用程序都可用的 Java 密钥库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆