简体   繁体   English

在证书的帮助下使用SSL套接字连接到服务器

[英]Connect to Server using SSL Socket with the help of certificate

I have a SSL Server which is written in Java using SSL Socket.Server is running properly.Now i need to connect to Server using client app. 我有一个使用SSL Socket用Java编写的SSL服务器。服务器运行正常。现在我需要使用客户端应用程序连接到服务器。

Client code: 客户代码:

System.setProperty("javax.net.ssl.trustStore", "C:\\cacerts.jks");        
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

try {
    SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("127.0.0.1", 800);

    InputStream inputstream = System.in;
    InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
    BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

    OutputStream outputstream = sslsocket.getOutputStream();
    OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
    BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);

    bufferedwriter.write("test"+"\n");
    bufferedwriter.flush();
    bufferedwriter.close();

} catch (Exception exception) {
    exception.printStackTrace();
}

In the above code,i'm using the same keystore file as truststore. 在上面的代码中,我正在使用与信任库相同的密钥库文件。

Is this good idea to use same keystore file as truststore at client side? 在客户端使用与信任库相同的密钥库文件是一个好主意吗? if not how can i connect to server in secure manner? 如果没有,我如何以安全的方式连接到服务器?

In the above code,i'm using the same keystore file as truststore. 在上面的代码中,我正在使用与信任库相同的密钥库文件。

No you're not. 不你不是。 You're only using a truststore. 您仅使用信任库。 If you're using a keystore, you must specify it: 如果您使用密钥库,则必须指定它:

System.setProperty("javax.net.ssl.keyStore", ...);        
System.setProperty("javax.net.ssl.keyStorePassword", ...);

and you should certainly not use the same file for both. 并且您当然应该对两个文件使用相同的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM