2013-06-25 10 views
0

我正在開發一個Java應用程序,我需要通過安全套接字向服務器發送幾個字符串(用戶和密碼),我必須使用我自己的由可信CA生成的證書,但我發現了一個異常基於SSLSockets的驗證java應用程序

服務器

class LoginServer { 

    private static final String CORRECT_USER_NAME = "Java"; 

    private static final String CORRECT_PASSWORD = "HowToProgram"; 

    private SSLServerSocket serverSocket; 

    public LoginServer() throws Exception { 
    SSLServerSocketFactory socketFactory = (SSLServerSocketFactory) SSLServerSocketFactory 
     .getDefault(); 
    serverSocket = (SSLServerSocket) socketFactory.createServerSocket(7070); 

    } 

    private void runServer() { 
    while (true) { 
     try { 
     System.err.println("Waiting for connection..."); 
     SSLSocket socket = (SSLSocket) serverSocket.accept(); 
     BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
     PrintWriter output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); 
     String userName = input.readLine(); 
     String password = input.readLine(); 

     if (userName.equals(CORRECT_USER_NAME) && password.equals(CORRECT_PASSWORD)) { 
      output.println("Welcome, " + userName); 
     } else { 
      output.println("Login Failed."); 
     } 
     output.close(); 
     input.close(); 
     socket.close(); 

     } catch (IOException ioException) { 
     ioException.printStackTrace(); 
     } 
    } 
    } 

    public static void main(String args[]) throws Exception { 
    LoginServer server = new LoginServer(); 
    server.runServer(); 
    } 
} 

客戶

class LoginClient { 
    public LoginClient() { 
    try { 
     SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); 
     SSLSocket socket = (SSLSocket) socketFactory.createSocket("localhost", 7070); 
     PrintWriter output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); 
     String userName = "MyName"; 
     output.println(userName); 
     String password = "MyPass"; 
     output.println(password); 
     output.flush(); 
     BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
     String response = input.readLine(); 
     System.out.println(response); 

     output.close(); 
     input.close(); 
     socket.close(); 
    } catch (IOException ioException) { 
     ioException.printStackTrace(); 
    } finally { 
     System.exit(0); 
    } 
    } 

    public static void main(String args[]) { 
    new LoginClient(); 
    } 
} 

這是輸出窗口中的結果:

javax.net.ssl.SSLHandshakeException: no cipher suites in common 
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) 
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1886) 
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276) 
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:266) 
    at sun.security.ssl.ServerHandshaker.chooseCipherSuite(ServerHandshaker.java:894) 
    at sun.security.ssl.ServerHandshaker.clientHello(ServerHandshaker.java:622) 
    at sun.security.ssl.ServerHandshaker.processMessage(ServerHandshaker.java:167) 
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868) 
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:804) 
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016) 
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312) 
    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 zzzz.LoginServer.runServer(LoginServer.java:35) 
    at zzzz.LoginServer.main(LoginServer.java:55) 

我希望你能幫助我。

非常感謝

回答

1

我看不出這是不可能的,除非有你沒有告訴我們更多的代碼,即以setEnabledCipherSuites()的調用。去掉它。

我也不明白爲什麼有必要使用SSL或登錄到運行在同一主機上的服務器。

+0

非常感謝,我寫了setEnabledCipherSuites()它的作品! – Litox

相關問題