![](/img/trans.png)
[英]Java “sun.security.validator.ValidatorException: PKIX path building failed” certificate validation error
[英]Sudden error: ValidatorException: PKIX path building failed
我的 Spring Boot 應用程序使用 REST 連接到不同的外部 URL,它們都具有相同的域。 我收到了此 URL 的證書和不同的憑據。
自 2 天以來,連接不再工作,因為我收到“ValidatorException PKIX 路徑構建失敗”錯誤(下面是異常的所有堆棧跟蹤)。
在我為每個 URL 創建一個特定的 REST 模板之前,我已經將 SSLContext 與特定的密鑰庫信息和客戶端憑據設置在一起。
如果我將以下信任存儲添加到 SSLContext,我設法修復了錯誤(我收到了一個 CA 文件):
// Create Trust Managers
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
Certificate ca = certificateFactory.generateCertificate(caInput);
String alias = ((X509Certificate) ca).getSubjectX500Principal().getName();
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null);
trustStore.setCertificateEntry(alias, ca);
TrustManager[] trustManagers = {new CustomTrustManager(trustStore)};
你能不能給我一個提示:
謝謝!
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://....": sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is 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
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:696)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:644)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:296)
此錯誤通常意味着您嘗試與之通信的服務器使用證書,其中 (1) 您沒有連接到客戶端的證書和 (2) 您沒有用於創建服務器的中間證書連接到您的客戶端的側證書。 通常,當證書是自簽名證書或由不在 Java 標准密鑰庫中的證書頒發機構簽名時,您會看到此錯誤。
為了確保來自客戶端的所有調用都具有 ,您應該將證書添加到應用程序使用的密鑰庫中。 這可以是標准的 java 密鑰庫,也可以是使用啟動參數連接到 JVM 的特定於應用程序的密鑰庫-Djavax.net.ssl.trustStore=<path to keystore>
和-Djavax.net.ssl.trustStorePassword=<password>
.
如果服務器將其證書更改為您的客戶端不知道的內容,則此錯誤可能會突然出現。
此問題發生在 IDE(如 Intelij)內部,只需在 IDE 之外的終端運行它即可。 要在 IDE 中運行或調試,應在 intelij 中添加證書。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.