【问题标题】:Getting error: PKIX path building failed: unable to find valid certification path to requested target出现错误:PKIX 路径构建失败:无法找到请求目标的有效证书路径
【发布时间】:2013-06-27 14:50:31
【问题描述】:

我正在尝试通过 Web 服务将 xml 发送到另一个系统。但是在尝试发送时,我收到以下错误。我已经安装了他们给我的证书。但它仍然无法正常工作。

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

【问题讨论】:

  • 您在哪里安装了证书?是自签名证书吗?
  • @Avi:我已经在 java keytool 中安装了证书。它不是自签名证书。

标签: java spring tomcat ssl certificate


【解决方案1】:

这个错误有两个可能的来源:

  • 对方正在使用真正不受信任的证书(自签名或由不受信任的 CA 签名),
  • 或对方未发送证书验证链(例如,在通往您受信任的 CA 的过程中存在中间签名证书,但 SSL 握手中不存在此证书)。

第一种情况的解决方案是将不受信任的 CA(或证书本身)添加到您的 JRE 信任库 (${java.home}/lib/security/cacerts) 或更好 - 创建您自己的信任库(升级 Java 时不会被删除)并将其提供给您的应用程序通过javax.net.ssl.trustStore JVM 属性。

第二种情况的解决方案是采用第一种情况的解决方案或更好 - 说服对方发送正确的证书链。

【讨论】:

    【解决方案2】:

    将证书添加到 JRE 信任库 @ ${java.home}/lib/security/cacerts 或者如果您有自己的信任库并在您的应用程序/JVM 中提供指向该信任库的路径。例如,一种可能的方式是

    或通过java代码

    import java.util.Properties;
    ...
        Properties systemProps = System.getProperties();
        systemProps.put("javax.net.ssl.keyStorePassword","passwordForKeystore");
        systemProps.put("javax.net.ssl.keyStore","pathToKeystore.ks");
        systemProps.put("javax.net.ssl.trustStore", "pathToTruststore.ts");
        systemProps.put("javax.net.ssl.trustStorePassword","passwordForTrustStore");
        System.setProperties(systemProps);
    ...
    

    更多详情请参考 RedHat site

    可能会帮助参考question

    【讨论】:

      【解决方案3】:

      我现在在 2020 年遇到了同样的问题,所以认为它可能对面临这个问题的人有所帮助。

      您可以在调用安全 API 之前使用以下握手方法代码 -

      public static void handshakeHttps() throws NoSuchAlgorithmException, KeyManagementException{
          // Create a trust manager that does not validate certificate chains
          TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
                  public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                      return null;
                  }
                  public void checkClientTrusted(X509Certificate[] certs, String authType) {
                  }
                  public void checkServerTrusted(X509Certificate[] certs, String authType) {
                  }
              }
          };
      
          // Install the all-trusting trust manager
          SSLContext sc = SSLContext.getInstance("SSL");
          sc.init(null, trustAllCerts, new java.security.SecureRandom());
          HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
      
          // Create all-trusting host name verifier
          HostnameVerifier allHostsValid = new HostnameVerifier() {
              public boolean verify(String hostname, SSLSession session) {
                  return true;
              }
          };
      
          // Install the all-trusting host verifier
          HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
          
      }
      

      如果使用后问题仍然存在,请告诉我。

      【讨论】:

        猜你喜欢
        • 2011-05-03
        • 2020-03-25
        • 2013-02-03
        • 2018-07-15
        • 2014-01-31
        相关资源
        最近更新 更多