gpt4 book ai didi

Java SSL套接字 : PKIX path building failed

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

我想使用 SSL 连接到我们的大学服务器。

我在配置文件夹中有以下文件:
- 客户政策
- uni_keystore.jks
- uni_server.cer
- uni_truststore.jks

client.policy 文件包含以下内容:

grant { 
permission java.security.AllPermission ;
};

与服务器的连接已建立,但是当我尝试从服务器读取时出现异常:

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

这是我连接到服务器的方式(我修剪了 try/catch block 以缩短代码):

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault();

String configPath = "C:/eclipse/workspace/uniproject/";

Properties systemProps = System.getProperties();
systemProps.put( "javax.net.ssl.trustStore", configPath+"uni_truststore.jks");
systemProps.put( "javax.net.ssl.keyStore", configPath+"uni_keystore.jks");
System.setProperties(systemProps);

s = (SSLSocket) factory.createSocket(UNI_ADDRESS, UNI_PORT);

我正在通过我的 VPN 连接到服务器。

这就是我尝试阅读的方式(我修剪了 try/catch block 以缩短代码):

InputStream istream = s.getInputStream();
BufferedReader reader = new BufferedReader( new InputStreamReader(istream) );

String data;
do {
// this lines throws exception
data = reader.readLine();
System.out.println(data);
}
while(data != null);

我没有对代码中的 client.policy 和 uni_server.cer 文件做任何事情。这可能是问题所在吗?我错过了什么?

谢谢。


还有两件事:

- 我安装了认证(在这篇文章之前,我没有)
- 我添加了这一行:

systemProps.put( "javax.net.ssl.keyStorePassword", "pass");

最佳答案

尝试像这样重新排序您的调用:

String configPath = "C:/eclipse/workspace/uniproject/";
Properties systemProps = System.getProperties();
systemProps.put( "javax.net.ssl.trustStore", configPath+"uni_truststore.jks");
systemProps.put( "javax.net.ssl.keyStore", configPath+"uni_keystore.jks");
System.setProperties(systemProps);

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
s = (SSLSocket) factory.createSocket(UNI_ADDRESS, UNI_PORT);

关于Java SSL套接字 : PKIX path building failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13504999/

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