2012-03-06 25 views
1

我試圖使用SSL來創建服務器,但我不斷收到以下錯誤:SSL服務器在Java中 - javax.net.ssl.SSLException

「服務器中止:javax.net.ssl.SSLException:無證書或密鑰對應於啓用的SSL密碼套件。「

我不知道我是否正確創建證書。這是我的代碼。 我是一個老TCP服務器轉換爲SSL服務器

// SSL Server 

import java.net.*; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import javax.net.ServerSocketFactory; 
import javax.net.ssl.SSLServerSocketFactory; 

public class SSL_Server { 


public static void main(String[] args) { 
    int port = 2018; 
    ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault(); 
    ServerSocket ssocket = null; 
    System.out.println("SSL_Server started"); 

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

    final ExecutorService threadPool = Executors.newCachedThreadPool(); 

    try { 
     ssocket = ssocketFactory.createServerSocket(port); 
     InetAddress myIP =InetAddress.getLocalHost(); 
     System.out.println(myIP.getHostAddress()); 

     while(true){ 
      Socket aClient = ssocket.accept(); 
      //create a new thread for every client 
      threadPool.submit(new SSL_ClientHandler(aClient)); 
     } 
    } 
    catch(Exception e) { 
     System.err.println("Server aborted:" + e); 
    } finally { 
     try{ 
      ssocket.close(); 
     } catch (Exception e){ 
      System.err.println("could not close connection properly" + e); 
     } 
    } 
    System.out.println("connection was closed successfully"); 
} 

}

+0

該消息的「服務器中止」部分是嚴格的發明。相關部分是您從異常中獲得的部分。我強烈建議你相應地修改你的標題。 – EJP 2012-03-07 00:33:46

回答

1

你應該設置配置默認SSLContext的屬性(因此默認SSLServerSocketFactory)得到它,因爲它會配置之前然後。

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

ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault(); 
ServerSocket ssocket = null; 
System.out.println("SSL_Server started"); 
2
System.setProperty("javax.net.ssl. keyStore","mySrvKeystore"); System.setProperty("javax.net.ssl. keyStorePassword","123456"); 

ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault(); 
ServerSocket ssocket = null; 
System.out.println("SSL_Server started");