当前位置:   article > 正文

解决javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException

javax.net.ssl.sslhandshakeexception: sun.security.validator.validatorexcepti

问题现象:

Java Spring应用发送数据报如下问题。

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

原因分析:

httpclient访问https资源时,会出现异常,与环境也有关系,有些机器请求正常。

解决方案:

1.复制附录代码到文本文件中,改名为InstallCert.java。然后在命令行中执行命令,编译InstallCert.java:

javac InstallCert.java

 2.再执行以下命令:

java InstallCert www.baidu.com

说明:网址处不需要输入(https://)信息,比如 www.baidu.com,只需域名即可。

随后服务器会返回认证的主题,发行方,加密方式等信息。

3.在这里输入1后,回车。

  1. Enter certificate to add to trusted keystore or 'q' to quit: [1]
  2. 1

在目录下会生成文件jssecacerts,自此认证过程结束。

替换证书:

1.windows系统:将生成的jssecacerts文件复制到 jdk的相应路径下即可。

windows系统一般需要首先找到jdk安装路径

D:\Java\jdk1.8.0_221\jre\lib\security

2.Linux系统:需要首先使用 scp 命令 将 jssecacerts 传输至服务器。

3.docker:jdk通常在这个地方

/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/security/

/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security/ (和上面其实一样)

替换完后,重启 docker 即可。

附录: 

  1. /*
  2. * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. *  - Redistributions of source code must retain the above copyright
  9. *    notice, this list of conditions and the following disclaimer.
  10. *
  11. *  - Redistributions in binary form must reproduce the above copyright
  12. *    notice, this list of conditions and the following disclaimer in the
  13. *    documentation and/or other materials provided with the distribution.
  14. *
  15. *  - Neither the name of Sun Microsystems nor the names of its
  16. *    contributors may be used to endorse or promote products derived
  17. *    from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. import java.io.*;
  32. import java.net.URL;
  33. import java.security.*;
  34. import java.security.cert.*;
  35. import javax.net.ssl.*;
  36. public class InstallCert {
  37. public static void main(String[] args) throws Exception {
  38. String host;
  39. int port;
  40. char[] passphrase;
  41. if ((args.length == 1) || (args.length == 2)) {
  42. String[] c = args[0].split(":");
  43. host = c[0];
  44. port = (c.length == 1) ? 443 : Integer.parseInt(c[1]);
  45. String p = (args.length == 1) ? "changeit" : args[1];
  46. passphrase = p.toCharArray();
  47. } else {
  48. System.out.println("Usage: java InstallCert [:port] [passphrase]");
  49. return;
  50. }
  51. File file = new File("jssecacerts");
  52. if (file.isFile() == false) {
  53. char SEP = File.separatorChar;
  54. File dir = new File(System.getProperty("java.home") + SEP + "lib" + SEP + "security");
  55. file = new File(dir, "jssecacerts");
  56. if (file.isFile() == false) {
  57. file = new File(dir, "cacerts");
  58. }
  59. }
  60. System.out.println("Loading KeyStore " + file + "...");
  61. InputStream in = new FileInputStream(file);
  62. KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
  63. ks.load(in, passphrase);
  64. in.close();
  65. SSLContext context = SSLContext.getInstance("TLS");
  66. TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  67. tmf.init(ks);
  68. X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0];
  69. SavingTrustManager tm = new SavingTrustManager(defaultTrustManager);
  70. context.init(null, new TrustManager[]{tm}, null);
  71. SSLSocketFactory factory = context.getSocketFactory();
  72. System.out.println("Opening connection to " + host + ":" + port + "...");
  73. SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
  74. socket.setSoTimeout(10000);
  75. try {
  76. System.out.println("Starting SSL handshake...");
  77. socket.startHandshake();
  78. socket.close();
  79. System.out.println();
  80. System.out.println("No errors, certificate is already trusted");
  81. } catch (SSLException e) {
  82. System.out.println();
  83. e.printStackTrace(System.out);
  84. }
  85. X509Certificate[] chain = tm.chain;
  86. if (chain == null) {
  87. System.out.println("Could not obtain server certificate chain");
  88. return;
  89. }
  90. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  91. System.out.println();
  92. System.out.println("Server sent " + chain.length + " certificate(s):");
  93. System.out.println();
  94. MessageDigest sha1 = MessageDigest.getInstance("SHA1");
  95. MessageDigest md5 = MessageDigest.getInstance("MD5");
  96. for (int i = 0; i < chain.length; i++) {
  97. X509Certificate cert = chain[i];
  98. System.out.println(" " + (i + 1) + " Subject " + cert.getSubjectDN());
  99. System.out.println("  Issuer  " + cert.getIssuerDN());
  100. sha1.update(cert.getEncoded());
  101. System.out.println("  sha1    " + toHexString(sha1.digest()));
  102. md5.update(cert.getEncoded());
  103. System.out.println("  md5    " + toHexString(md5.digest()));
  104. System.out.println();
  105. }
  106. System.out.println("Enter certificate to add to trusted keystore or 'q' to quit: [1]");
  107. String line = reader.readLine().trim();
  108. int k;
  109. try {
  110. k = (line.length() == 0) ? 0 : Integer.parseInt(line) - 1;
  111. } catch (NumberFormatException e) {
  112. System.out.println("KeyStore not changed");
  113. return;
  114. }
  115. X509Certificate cert = chain[k];
  116. String alias = host + "-" + (k + 1);
  117. ks.setCertificateEntry(alias, cert);
  118. OutputStream out = new FileOutputStream("jssecacerts");
  119. ks.store(out, passphrase);
  120. out.close();
  121. System.out.println();
  122. System.out.println(cert);
  123. System.out.println();
  124. System.out.println("Added certificate to keystore 'jssecacerts' using alias '" + alias + "'");
  125. }
  126. private static final char[] HEXDIGITS = "0123456789abcdef".toCharArray();
  127. private static String toHexString(byte[] bytes) {
  128. StringBuilder sb = new StringBuilder(bytes.length * 3);
  129. for (int b : bytes) {
  130. b &= 0xff;
  131. sb.append(HEXDIGITS[b >> 4]);
  132. sb.append(HEXDIGITS[b & 15]);
  133. sb.append(' ');
  134. }
  135. return sb.toString();
  136. }
  137. private static class SavingTrustManager implements X509TrustManager {
  138. private final X509TrustManager tm;
  139. private X509Certificate[] chain;
  140. SavingTrustManager(X509TrustManager tm) {
  141. this.tm = tm;
  142. }
  143. public X509Certificate[] getAcceptedIssuers() {
  144. throw new UnsupportedOperationException();
  145. }
  146. public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  147. throw new UnsupportedOperationException();
  148. }
  149. public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  150. this.chain = chain;
  151. tm.checkServerTrusted(chain, authType);
  152. }
  153. }
  154. }

另详见:https://blog.csdn.net/chaishen10000/article/details/82992291

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/372348
推荐阅读
相关标签
  

闽ICP备14008679号