zoukankan      html  css  js  c++  java
  • 自签证书:请求第三方自签名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服务

  • 相关阅读:
    大道至简读后感(第二章)
    大道至简读后感
    将课程中的所有动手动脑的问题以及课后实验性的问题,整理成一篇文档
    python之基础
    python之面向对象
    python之网络编程
    python之函数
    Managing SharePoint 2010 Farm Solutions with Windows PowerShell
    Oracle RMAN vs. Export?
    转帖在oracle中自动大批量生成测试数据
  • 原文地址:https://www.cnblogs.com/achengmu/p/14452900.html
Copyright © 2011-2022 走看看