pkix路径生成失败:suncertpathbuilderexception:找不到请求的有效证书路径

at0kjp5o  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(284)

我使用的是eclipse,在尝试执行这个函数时,我发现了以下错误。
我想发送一个get请求以及证书和密钥。我可以下载任何格式的证书,所以这不是问题。我知道我需要将这个添加到javakeystone中,但是在尝试了各种建议之后,我仍然无法修复这个问题。

  1. public String sendGET(String GET_URL, String authStringEnc) throws IOException {
  2. try {
  3. KeyStore ks = KeyStore.getInstance("JKS");
  4. FileInputStream fis = new FileInputStream("src/com/resources/ece-cyberark-cert.jks");
  5. ks.load(fis, "5<@7wBj[Ht()~GRf".toCharArray());
  6. KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
  7. kmf.init(ks, "5<@7wBj[Ht()~GRf".toCharArray());
  8. SSLContext sc = SSLContext.getInstance("TLS");
  9. sc.init(kmf.getKeyManagers(), null, null);
  10. URL obj = new URL(GET_URL);
  11. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  12. if (con instanceof HttpsURLConnection) {
  13. ((HttpsURLConnection)con)
  14. .setSSLSocketFactory(sc.getSocketFactory());
  15. }
  16. con.setRequestMethod("GET");
  17. con.setRequestProperty("User-Agent", USER_AGENT);
  18. con.setRequestProperty("Authorization", "Basic " + authStringEnc);
  19. con.setRequestProperty("Content-Type", "application/json");
  20. int responseCode = con.getResponseCode();
  21. System.out.println("GET Response Code :: " + responseCode + " :: " + GET_URL);
  22. if (responseCode == HttpURLConnection.HTTP_OK) { // success
  23. BufferedReader in = new BufferedReader(new InputStreamReader(
  24. con.getInputStream()));
  25. String inputLine;
  26. StringBuffer response = new StringBuffer();
  27. while ((inputLine = in.readLine()) != null) {
  28. response.append(inputLine);
  29. }
  30. in.close();
  31. con.disconnect();
  32. // print result
  33. return response.toString();
  34. } else {
  35. // return failed requests response code
  36. return "GET request not worked :: GET Response Code :: " + responseCode + " :: + GET_URL";
  37. }
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. return "Exceptionn";
  41. }
  42. }

下面是错误-

  1. 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
  2. at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
  3. at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1946)
  4. at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:316)
  5. at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:310)
  6. at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1639)
  7. at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:223)
  8. at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1037)
  9. at sun.security.ssl.Handshaker.process_record(Handshaker.java:965)
  10. at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1064)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题