---
title: "\"PKIX path building failed\"的问题分析"
author: "DavisWang"
date: "2015-03-25T15:19:42Z"
tags: []
categories: ["互联网"]
slug: "pkix-path-building-failed"
draft: false
lastmod: "2015-03-25T17:14:24Z"
id: 2195480
url: "https://daviswang.iteye.com/blog/2195480"
---

在通过java程序访问https服务时常常会遇到下面的异常 Caused by: sun.security.validator.Va lidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

     **原因分析：**

     此异常是由于jre不能验证服务端的证书所致，归其原因有下面两种可能：

     1）服务端使用的是自签名证书或证书签发机构不是公认的权威机构。这种情况由于在\\jre\\lib\\security\\cacerts中找不到CA证书所以不能验证服务器返回证书的真实性。注通过命令 keytool -list -keystore cacerts 可查看cacerts中的可信CA。

     2）服务端使用的证书缺少中间证书即证书链不完整。这种情况通过浏览器访问一般是正常的，因为大部分浏览器会自动从证书签发机构下载中间证书，这样就可以补齐证书链，而java的security库并不会完成中间证书的下载，所以不能验证服务器返回证书的真实性。

    3）还有一种可能就是人为失误，服务器证书与中间证书或CA证书不匹配。这个只能是排除上面两种可能后本地验证证书链是否正确。

     **解决方法：**

     对于第一种情况需要把自签名的证书或缺少的CA证书导入到cacerts；对于第二种情况需要在服务端补齐证书链，具体操作方法参见：

     在nginx上配置证书链 [https://scottlinux.com/2013/09/02/how-to-configure-ssl-certificate-chain-for-nginx/](https://scottlinux.com/2013/09/02/how-to-configure-ssl-certificate-chain-for-nginx/)

     在F5上配置证书链 [http://www.entrust.net/knowledge-base/technote.cfm?tn=8268](http://www.entrust.net/knowledge-base/technote.cfm?tn=8268)