gpt4 book ai didi

Java 缓存 SSL 失败——我能以某种方式刷新这些吗

转载 作者:太空宇宙 更新时间:2023-11-03 13:00:01 25 4
gpt4 key购买 nike

我正在尝试在单元测试中测试我的 SSL 实现,但有一个场景我不太明白。

当我连接到主机一次并失败时,即使它具有正确的证书,随后的每个连接也将失败。我假设在某个地方我必须刷新缓存。

这是我的代码,服务器和客户端都在本地运行。我对 trustStore 和 keyStore 使用一个 jks-File。无论初始错误是什么,都会发生错误,下次我总是会遇到第一个错误。

如果我不执行第一个请求,第二个请求就会起作用。

如果您想知道这里的用例是什么,我们有一些本地服务器使用来自内部 PKI 的 https 证书,当有人错误配置服务器或证书时,我们显然希望能够更改它们,无需关闭整个虚拟机。

    //attempt a connection without certificates, will fail
try (final InputStream stream = new URL("https://localhost:" + port).openStream()){
System.out.println(IOUtils.toString(stream, Charset.defaultCharset()));
} catch (IOException e){
System.out.println("Failed to load: " + StackTraceUtil.getStackTrace(e));
}

//copies the jks file to a temporary location
final File jksFile = copyJKSFile();

//ignore host names, running locally, won't use this in production
HttpsURLConnection.setDefaultHostnameVerifier((hostname, sslSession) -> hostname.equalsIgnoreCase("localhost"));

//set the system properties
System.setProperty("javax.net.ssl.keyStore", jksFile.getAbsolutePath());
System.setProperty("javax.net.ssl.keyStorePassword", password);
System.setProperty("javax.net.ssl.trustStore", jksFile.getAbsolutePath());
System.setProperty("javax.net.ssl.trustStorePassword", password);

//this should work now
try (final InputStream stream = new URL("https://localhost:" + port).openStream()){
System.out.println(IOUtils.toString(stream, Charset.defaultCharset()));
} catch (IOException e){
System.out.println("Failed to load: " + StackTraceUtil.getStackTrace(e));
}

感谢您的帮助!

最佳答案

所以我找到了一个解决方案,我想我会分享它以防其他人在某个时候遇到这个问题。

javax.net.ssl.HttpsURLConnection 使用 javax.net.ssl.SSLSocketFactory 加载 Key- 和 TrustStore,后者使用 javax .net.ssl.SSLContext 内部。当您不覆盖任何内容时,它会使用默认实现,加载文件并且一旦加载就无法重置。

所以我所做的不是使用默认实现,而是设置我自己的 SSLContext,当我知道文件会更改时。

final SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, null, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

如果您想使用旧版本的 TLS,完整列表应为 here

关于Java 缓存 SSL 失败——我能以某种方式刷新这些吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54671365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com