Java https服务器证书认证问题解决方案

Java https连接的”unable to find valid certification path to requested target”,”PKIX path building failed”错误

问题原因

这个问题的是由于Java自带的根证书库中不包含HTTPS服务器上的根证书,因此无法得到认证。

解决方案

比较容易实现的方案有两种:

  1. 导入服务器证书到本地Java环境
  2. 代码中忽略证书信任问题

由于第二种方案会导致安全性问题,因此并不推荐。

证书导入注意事项

将服务器的根证书导入到Java运行环境的根证书库中,能解决对应的服务器https连接问题。

实现方式是使用$JAVA_HOME/jre/bin下的keytool工具将服务器端的证书添加到jre/lib/security/cacerts文件中。 
需要注意的是:

  1. 确定当前Java程序所用的java运行环境jre的路径(可能为jdk下的jre,也可能是单独的jre)。
  2. 确定有jre/lib/security/cacerts文件的写入权限(可以用管理员权限运行keytool)。

详细步骤:

1. 获取服务器端的证书文件

可以使用浏览器打开服务器网站页面,然后导出证书文件,假设导出的证书文件为test.crt 。

2. 生成keystore文件

利用keytool生成密钥文件keystore:

<code class="language-bash hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">keytool -importcert -noprompt -trustcacerts -alias test -file test.cer -keystore ~/mykeystore</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

这里会要求设置口令,设置后请记住口令 。

3. 导入证书到Java运行时环境

将证书导入jre/lib/security/cacerts:

<code class="language-bash hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">keytool -importkeystore -srckeystore ~/keystore -destkeystore [path_to_jre]/lib/security/cacerts</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

会要求输入目标密钥库口令(也就是jre/lib/security/cacerts 的口令,默认是changeit或者changeme),以及源密钥库口令(之前设置的口令).

最后会显示是否导入成功。如果成功则重启Java程序。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值