gpt4 book ai didi

Java SSL cacerts 仅在本地计算机上工作

转载 作者:行者123 更新时间:2023-12-01 22:45:39 28 4
gpt4 key购买 nike

我的程序中有一些代码设置套接字连接,然后写入远程服务器。

        //Get Socket, set timeout, and initialize I/O streams
SSLSocket sock = null;
try {
InetAddress host = InetAddress.getByName("212.32.251.215");
int port = 55540;
SSLSocketFactory sslsocketfactory = (SSLSocketFactory)
SSLSocketFactory.getDefault();
sock = (SSLSocket) sslsocketfactory.createSocket(host, port);
} catch (Exception e) {
e.printStackTrace();
}
sock.setSoTimeout(100000);
OutputStream out = sock.getOutputStream();
BufferedInputStream in = new BufferedInputStream(sock.getInputStream());

//Get Message (Compress and Encrypt)
byte[] msg = buildMsg();

//Send message to Server
out.write(msg);
out.flush();

在我的本地计算机上,一切运行顺利,但在我的云服务器上,我遇到了一个熟悉的错误 – 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

我已确保证书位于 cacerts 中 keytool -list -v -keystore /etc/pki/java/cacerts -alias myalias 。我还检查了此 cacerts 是否是我的 java 应用程序的默认信任库 System.getProperty("javax.net.ssl.trustStore");返回 /etc/pki/java 。话虽这么说,我什至尝试过System.setProperty("javax.net.ssl.trustStore", "/etc/pki/java/cacerts");正如某人所建议的,他说 Tomcat 忽略了他们的默认 trustStore。另外,是的,这是我的服务器中所有 jre 版本的 cacerts 位置,每个 jre 都有一个 cacerts,但它是指向此规范位置的符号链接(symbolic link)。

此时我不知道为什么我仍然收到此错误;任何建议或解决方案表示赞赏。谢谢。

最佳答案

我最终通过显式创建一个 trustStore 来解决这个问题,而不是依赖 java 来找到它。

        KeyStore trustStore = KeyStore.getInstance("JKS");
trustStore.load(new FileInputStream(mypath), password.toCharArray());

TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(trustStore);

SSLContext sslcontext = SSLContext.getInstance("SSLv3");
sslcontext.init(null, tmf.getTrustManagers(), null);

SSLSocketFactory sslsocketfactory = sslcontext.getSocketFactory();

InetAddress host = InetAddress.getByName("212.32.251.215");
int port = 55540;

sslsocket = (SSLSocket) sslsocketfactory.createSocket(host, port);

关于Java SSL cacerts 仅在本地计算机上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58474862/

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