gpt4 book ai didi

java - 在java中与支持ssl的服务器通信

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

我有一个关于 sslConnectionin java 的问题,我为客户端编写了下面的代码(这个应用程序必须连接到服务器,我有支持 ssl 的服务器)但是我得到这个错误“javax.net.ssl.SSLException:连接有已关闭:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径”

我该如何解决我的问题?

  public static void main(String[] args)  {
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
PrintStream out = System.out;
SSLSocketFactory f =
(SSLSocketFactory) SSLSocketFactory.getDefault();
try {
SSLSocket c =
(SSLSocket) f.createSocket("192.168.10.38", 7701);
printSocketInfo(c);
System.out.println("End printSocketInfo");
c.startHandshake();
System.out.println("HandShake oK");

BufferedWriter w = new BufferedWriter(
new OutputStreamWriter(c.getOutputStream()));
BufferedReader r = new BufferedReader(
new InputStreamReader(c.getInputStream()));
String m = null;
// String m=
while ((m=r.readLine())!= null) {
System.out.println("11111");
out.println(m);
m = in.readLine();
System.out.println("M is: "+ m);
w.write(m,0,m.length());
w.newLine();
w.flush();
}
w.close();
r.close();
c.close();
} catch (IOException e) {
System.err.println(e.toString());
}



}

private static void printSocketInfo(SSLSocket s) {
System.out.println("Socket class: "+s.getClass());
System.out.println(" Remote address = "
+s.getInetAddress().toString());
System.out.println(" Remote port = "+s.getPort());
System.out.println(" Local socket address = "
+s.getLocalSocketAddress().toString());
System.out.println(" Local address = "
+s.getLocalAddress().toString());
System.out.println(" Local port = "+s.getLocalPort());
System.out.println(" Need client authentication = "
+s.getNeedClientAuth());
SSLSession ss = s.getSession();
System.out.println(" Cipher suite = "+ss.getCipherSuite());
System.out.println(" Protocol = "+ss.getProtocol());
}

我有一个证书文件,如何使用这个证书?

最好的问候

最佳答案

是否是自签名证书

如果是,那么你有两个选择第一个选项:

在全局 Java 证书信任库中导入证书颁发机构证书。这家店位于

%Java Installation%/jre/lib/security/cacerts

要导入它,您可以使用 java 安装附带的 Keytool 命令

keytool -import -alias keyName -file yourcertificate.crt -keystore cacerts

优点:

  • 无需修改代码。
  • 部署简单

缺点:

  • cacert 文件将在下一次 java 更新中被覆盖。你必须再次导入证书。
  • 需要管理权限(在 Linux 和 Windows 上)

第二个选项:

如果您想绕过证书验证,请遵循 Java: Overriding function to disable SSL certificate check

否则为您的程序创建新的信任库

keytool -import -alias keyName -file yourcertificate.crt -keystore yourtruststore

此命令将要求输入密码两次。输入您想要的任何密码,并为任何问题输入"is"将在当前目录中创建一个名为“yourtruststore”的文件

现在你需要在你的程序中使用这个信任库

SSLSocketFactory sslFactory = null;
InputStream trustStore = null;
KeyStore keyStore = null;
trustStore = new FileInputStream("<your trust store absolute path>");
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(trustStore, "<your trust store password>".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, tmf.getTrustManagers(), null);
sslFactory = ctx.getSocketFactory();

你可以使用这个套接字工厂来打开新的套接字

关于java - 在java中与支持ssl的服务器通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23993092/

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