自签证书:请求第三方自签名https证书报错:sun.security.validator.ValidatorException: PKIX path building failed
自签证书:请求第三方自签名https证书报错:
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
使用 ssl 连接时,遇到不信任的证书,应用程序一般都会拒绝连接。
浏览网站时,我们可以通过在浏览器的设置中导入证书,把证书加入到信任列表中。
而在 JAVA 直接进行 SSL 连接应用时,默认没有一个界面来导入证书。JAVA 进行不信任的 ssl 连接时,会报如下异常:
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
发现报错后,才知道对方用的是自签名证书。
于是向对方要了:server.cer 证书
原理:
Java 使用了一种叫 keystore 的文件来存储证书 (默认是位于 $JAVA_HOME/lib/security/cacerts ) 。
该文件使用 keytool 工具去管理 (该工具默认位于 $JAVA_HOME/bin/keytool )。
keytool 工具的使用不在这里展开,网上有比较详细的说明。这里主要列举几个会用到的命令。
列出 keystore 中的证书
keytool -list
默认情况下,它会在你的 $HOME 目录下产生一个空的 .keystore 文件。如要指定 Java 正在用的 keystore 文件,使用以下参数
keytool -list -keystore $JAVA_HOME/lib/security/cacerts
注意一下, keystore 文件都受 密码 保护。生成新的 keystore 文件时,会要求你输入一个新密码;而当访问一个已有的 keystore 文件时,会要求你验证密码。
$JAVA_HOME/lib/security/cacerts 的默认密码为 “changeit” !!!
$JAVA_HOME/lib/security/cacerts 的默认密码为 “changeit” !!!
$JAVA_HOME/lib/security/cacerts 的默认密码为 “changeit” !!!
重要的事情说三遍!!!
我的java环境目录是:
/home/centos/jdk1.8.0_161
导入证书一:
keytool -import -alias <证书别名> -keystore $JAVA_HOME/jre/lib/security/cacerts -file your.crt
导入证书二:
我是进入到:/home/centos/jdk1.8.0_161/jre/lib/security,然后执行
/home/centos/jdk1.8.0_161/bin/keytool -import -alias payServer -keystore cacerts -file /root/server.cer
安装时,请输入:yes
最后一步
重启web服务
相关文章
- https-配置使用HTTPS的ASP.NET Web应用
- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)
- 【异常】Application failed to start due to an exception org.springframework.beans.factory.BeanCurrentlyInCreationException
- Solidworks安装完成提示failed to load slderresu.dll怎么办
- python3:用pip安装第三方库时报错Command "python setup.py egg_info" failed with error code的解决
- WSL(Linux子系统) gedit 报错 failed to commit changes to dconf: Failed to execute child process “dbus-launch” (No such file or directory)
- 解决yarn build时报错:内存溢出FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed
- ios多语言(国际化):read failed: the data couldn‘t be read because it isn‘t in the correct format
- 成功解决Fit Failed Warning: Estimator fit failed. The score on this train-test partition for these param
- 已解决(MongoDB安装报错)Service ‘MongoDB Server (MongoDB)’ (MongoDB) failed tostart. Verify that you have su
- 解决办法:git错误 error: failed to push some refs to 'https://github.com/...
- launch failed.Binary not found in Linux/Ubuntu解决方案
- Python使用requests時遇到Failed to establish a new connection
- Failed to start mongod.service: Unit mongod.service not found
- 解决办法:git错误 error: failed to push some refs to 'https://github.com/...
- [Git & GitHub] 解决办法:error: failed to push some refs to 'https://github.com/xxxx.git'