spring boot https 接口unable to find valid certification path to requested target
时间: 2023-11-06 21:01:19 浏览: 49
您遇到的问题是由于缺乏有效的证书路径而导致的。解决这个问题的方法是禁用SSL验证。您可以在您的Spring Boot应用程序中创建一个@Configuration类,并添加一个@Bean方法来禁用SSL验证。下面是一个示例代码:
@Configuration
public class MyAppConfig {
@Bean
public Boolean disableSSLValidation() throws Exception {
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate;
}
}}, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return true;
}
}
这段代码将禁用SSL验证,并允许在请求时不进行证书验证。请确保在使用此解决方案之前仔细评估风险,以确保您的应用程序的安全性。