问题现象
前些天第三方公司同我们公司开发的业务系统对接,调用我们系统的https接口时出现如下日志提示:
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

问题分析
几经周折,历经磨难后发现,此类问题一般是证书本身或者jdk版本问题导致的。解决问题的办法也有几种。
第一种办法:重新申请更换符合安全规范的证书。(推荐)
第二种办法:重写SSLSocketFactory类,信任所有所有证书。
public class HttpsClientUtil { private static ThreadSafeClientConnManager cm = null; // 多连接的线程安全的管理器 private static int MAX_TOTAL = 500; // 最大连接数 private static int defaultMaxConnection = 100; // 默认最大 主机连接数 public final static int CONNECT_TIMEOUT = 10000; // 连接超时时间 public final static int SOCKET_TIMEOUT = 90000; // 读取数据超时时间 static { // 设置访问协议 SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); try { schemeRegistry.register(new Scheme("https", 443, getSSLSocketFactory())); } catch (KeyManagementException e1) { e1.printStackTrace(); } catch (NoSuchAlgorithmException e1) { e1.printStackTrace(); } cm = new ThreadSafeClientConnManager(schemeRegistry); try { cm.setMaxTotal(MAX_TOTAL); // 每条通道的并发连接数设置(连接池) cm.setDefaultMaxPerRoute(defaultMaxConnection); } catch (NumberFormatException e) { e.printStackTrace(); } } public static HttpClient getHttpsClient() { HttpParams params = new BasicHttpParams(); // HTTP 协议的版本,1.1/1.0/0.9 params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); /* 连接超时 */ HttpConnectionParams.setConnectionTimeout(params, CONNECT_TIMEOUT); /* 请求超时 */ HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT); return new DefaultHttpClient(cm, params); } /** * 设置信任所有证书 * * @return * @throws KeyManagementException * @throws NoSuchAlgorithmException */ private static SSLSocketFactory getSSLSocketFactory() throws KeyManagementException, NoSuchAlgorithmException { // SSLContext ctx = SSLContext.getInstance("SSL"); SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);// 信任所有域名主机 return ssf; } public static void release() { if (cm != null) { cm.shutdown(); } }}
第三种办法:把安全证书导入到java中cacerts证书库。

废话不多说,直接上干货。
第一步是要下载证书
建议使用谷歌浏览器,如下图所示,点击“小锁”。

证书下载
在“证书”页面,点击“复制到文件”按钮,选择https证书存放到的目录位置。

复制证书到文件
在“证书导出向导”页面,选择base64编码。

选择编码
导入证书
切换到jdk jre的/lib/security/下,执行如下命令:
keytool -import -alias test -keystore cacerts -file D://test.cer
说明:
-alias 指定别名
-keystore 指定存储文件
-file 指定证书文件所在的目录
注意:当切换到 cacerts 文件所在的目录时,才可指定 -keystore cacerts,否则应该指定全路径。此时命令行会提示你输入cacerts证书库的密码,敲入changeit即可,这是java中cacerts证书库的默认密码。
库密钥口令输入:changeit
是否信任:y

证书导入成功。
查看证书,密钥默认是changeit
keytool -list -keystore cacerts -alias test
更新证书时,要先删除原来的证书,然后导入新的证书
keytool -list -keystore cacerts keytool -delete -alias test -keystore cacerts keytool -import -alias test -file mytest.cer -keystore cacerts -trustcacerts