服务器聊天 java - SSLException

Server chat java - SSLException

我被困在一个严肃的地方...,我正在尝试创建一个安全的服务器。所以我不得不修改我的代码以便能够以安全的方式发送,问题如下。

它向我显示错误:javax.net.ssl.SSLException: Connection has closed: javax.net.ssl.SSLException: Unsupported or unrecognized SSL message

我已经通过 keytool 制作了一个证书,但我不觉得它有效

我找遍了所有地方,但找不到解决问题的方法...

这是我的代码:

服务器:

System.setProperty("javax.net.ssl.keyStore", "test.store");
        System.setProperty("javax.net.ssl.keyStorePassword", "password");
        ServerSocketFactory serverSocketFactory =
      ServerSocketFactory.getDefault();
    ServerSocket    serverSocket =
      serverSocketFactory.createServerSocket(4444);
        System.out.println("Started...");
        while(true){
            new ServerThread(serverSocket.accept()).start();
        }

客户

System.setProperty("javax.net.ssl.trustStore", "test.store");
        SocketFactory socketFactory = SSLSocketFactory.getDefault();
        Socket socket  = socketFactory.createSocket("localhost", 4444);
        BufferedReader bf1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter username");
        writer.println(bf.readLine());
        String message = null;
        while(true){
            System.out.println("enter message send to server");
            message = bf.readLine();
            if(message.equals("quit")){
                socket.close();
                break;
            }
            writer.println(message);
            System.out.println("message from server : " + bf1.readLine());
        }

我不认为给你 serverThread 很重要,因为它没有用。

谢谢,祝你有愉快的一天:)

您的服务器未在 SSL 中侦听,因为您调用了错误的 factory。你需要 SSLServerSocketFactory 来代替。尝试

ServerSocketFactory serverSocketFactory = SSLServerSocketFactory.getDefault();