SSL Server in Java - javax.net.ssl.SSLException



我正在尝试使用SSL创建服务器,但不断收到以下错误:

"服务器中止: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");
}

}

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");

您应该在获取之前设置配置默认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");

相关内容

最新更新