【发布时间】:2022-01-17 08:48:16
【问题描述】:
我正在尝试信任所有证书,但当我尝试连接到端点时,我仍然无法找到证书路径
请在下面找到我的代码
我创建了一个 httpurl 连接并使用 SSL 上下文启动以加载 MTLS 连接的证书。
然后我尝试使用 httpurl 连接连接到端点。
@Service
@Slf4j
@RequiredArgsConstructor
public class OcspHttpClient {
private final OcspProperties ocspProperties;
private static final int MAX_CONNECTION_OCSP = 100;
private SSLSocketFactory getSSLContext() {
try {
System.setProperty("http.keepAlive", "true");
System.setProperty("http.maxConnections", Integer.toString(MAX_CONNECTION_OCSP));
FileInputStream stream = new FileInputStream(ocspProperties.getTrustStore().getPath());
KeyStore store = KeyStore.getInstance("JKS");
store.load(stream, ocspProperties.getTrustStore().getPassword().toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(store, ocspProperties.getTrustStore().getPassword().toCharArray());
KeyManager[] kms = kmf.getKeyManagers();
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kms, trustAllCerts, null);
return SSLContext.getDefault().getSocketFactory();
} catch (Exception ex) {
log.info("Error in creation of the default connection Factory");
return HttpsURLConnection.getDefaultSSLSocketFactory();
}
}
public byte[] getResponse(byte[] request, String url) throws IOException {
HttpsURLConnection con;
if (StringUtils.isBlank(url) || StringUtils.isEmpty(url)) {
log.info("url field not set -- setting the value", ocspProperties.getUrl());
url = ocspProperties.getUrl();
}
URL ocspUrl = new URL(url);
if (ocspProperties.getProxy().isEnabled()) {
log.info("Proxy Enabled for the ocsp Call");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ocspProperties.getProxy().getHost(), ocspProperties.getProxy().getPort()));
con = (HttpsURLConnection) ocspUrl.openConnection(proxy);
} else {
log.info("Proxy Disabled for the ocsp Call");
con = (HttpsURLConnection) ocspUrl.openConnection();
}
con.setSSLSocketFactory(getSSLContext());
con.setRequestProperty("Content-Type", "application/ocsp-request");
con.setRequestProperty("Accept", "application/ocsp-response");
con.setDoOutput(true);
con.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession sslSession) {
return true;
}
});
OutputStream out = con.getOutputStream();
DataOutputStream dataOut = new DataOutputStream(new BufferedOutputStream(out));
dataOut.write(request);
dataOut.flush();
dataOut.close();
InputStream in = (InputStream) con.getContent();
return IOUtils.toByteArray(in);
}
}
然后得到以下错误:不确定如何信任所有证书 - 请对此提供帮助
"message":"PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target","stack_trace":"sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target\r\n\tat sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)\r\n\tat sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)\r\n\tat java.security.cert.CertPathBuilder.build(CertPathBuilder.java:297)\r\n\tat sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:434)\r\n\t..
【问题讨论】:
标签: java spring spring-boot spring-mvc resttemplate