5

我终于设法让 SSL 在客户端和我的服务器之间工作......至少直接从 Netbeans 运行它时。

客户端网络初始化代码:

private Network(final String hostname, final int port) {
    try {
        URL resource = getClass().getResource("/truststore/TCGtruststore.jks");
        if (resource == null) {
            Controller.getInstance().write(MessageType.DEBUG, "Could not load trust store.");
            throw new IllegalStateException("network.Network: Could not load trust store.");
        }
        else {
            Controller.getInstance().write(MessageType.LOG, "Loaded trust store.");
        }
        System.setProperty("javax.net.ssl.trustStore", resource.getPath());  
        System.setProperty("javax.net.ssl.trustStorePassword", "tcgadmin"); 

        Socket baseSocket = new Socket();
        baseSocket.connect(new InetSocketAddress(hostname, port), Config.TIMEOUT);
        SSLSocketFactory socketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
        clientSocket = (SSLSocket)socketFactory.createSocket(baseSocket, hostname, port, true);
        clientSocket.startHandshake();
        out = new PrintWriter(clientSocket.getOutputStream(), true);     
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        connected = true;
    } catch (IOException ex) { 
        connected = false;
    }

    waitForServer = new HashMap<>();
}

服务器网络初始化代码:

public Server(final int port) {
    this.port = port;
    try {
        URL resource = getClass().getResource("/keystore/TCGkeystore.jks");
        if (resource == null) {
            throw new IllegalStateException("server.Server: Could not load key store.");
        }
        System.setProperty("javax.net.ssl.keyStore", resource.getPath());  
        System.setProperty("javax.net.ssl.keyStorePassword", "tcgadmin"); 

        //serverSocket = new ServerSocket(port);
        serverSocket = (SSLServerSocket)SSLServerSocketFactory.getDefault().createServerSocket(port);
    } catch (IOException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    }        
}

但是,当我清理并构建它并在 Netbeans 之外运行它时,它会中断。令人惊讶的是,客户端仍然表现良好,但服务器开始对这条消息表现得很奇怪:

mei 20, 2013 4:36:16 PM server.ServerConnectionReceiver run
SEVERE: null
javax.net.ssl.SSLException: Received fatal alert: internal_error
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
    at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1977)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1093)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1328)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:882)
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:102)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.BufferedReader.fill(BufferedReader.java:154)
    at java.io.BufferedReader.readLine(BufferedReader.java:317)
    at java.io.BufferedReader.readLine(BufferedReader.java:382)
    at server.ServerConnectionReceiver.run(ServerConnectionReceiver.java:45)
    at java.lang.Thread.run(Thread.java:722)

来自客户端的命令行代码:

[16:36:15] [Log] Loaded trust store.
[16:36:16] [Log] Could not establish a connection with the server.

有没有人知道出了什么问题?

  • 当服务器也从 netbeans 和客户端运行时,一切都很好。
  • 当服务器通过命令行从 netbeans 和客户端运行时,服务器会给出异常。
  • 当我尝试从命令行运行服务器时,它给出了一个错误。(我正要测试它是否可以从命令行同时使用,但不幸的是我不能只测试它)

问候。

4

0 回答 0