• 自签证书:请求第三方自签名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服务

  • 相关阅读:
    阅读笔记7
    阅读笔记6
    架构阅读笔记5
    软件质量属性——易用性课堂讨论问题总结
    Git 的 .gitignore 配置
    zookeeper的简单搭建,java使用zk的例子和一些坑
    MySQL中有关TIMESTAMP和DATETIME的对比
    Mysql 如何设置字段自动获取当前时间,附带添加字段和修改字段的例子
    spring boot注入error,Consider defining a bean of type 'xxx' in your configuration问题解决方案
    net start命令发生系统错误5和错误1058的解决方法
  • 原文地址:https://www.cnblogs.com/achengmu/p/14452900.html
Copyright © 2020-2023  润新知