exception - SSL 服务器异常 : javax.net.ssl.SSLException

标签 exception ssl

我正在用 Java 创建 SSL 服务器和客户端。该程序的重​​点是模仿电影院的节目。我可以建立连接,但是当我尝试“预订”座位时,程序崩溃了。我收到以下错误:

服务器中止:javax.net.ssl.SSLException:连接已关闭:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效认证路径

这是我的服务器代码

// 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;

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

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

以下是我的客户端代码

//SSL Client
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocketFactory;

public class TCP_Client {


public static void main(String[] args) throws IOException{
//  SSL_Client newClient = new SSL_Client();
//  Lock lock = new ReentrantLock();
    boolean validInput = false;

    BufferedReader din;
    PrintStream pout;

    int port = 2018;
    BufferedReader stdinp = new BufferedReader(new InputStreamReader(System.in));
    String line = "done";
    StringTokenizer st;
    String hostname; 
    String task = "done";


    if(args.length>0)
        hostname = args[0];
    else
        hostname = "localhost";

    SocketFactory socketFactory = SSLSocketFactory.getDefault();
    //Socket socket = socketFactory.createSocket(hostname, port);

    while(true)
    {
        try{
            //read input
            while(!validInput)
            {
                System.out.println("Please enter a valid command or 'done' to finish.");
                line = stdinp.readLine();
                st = new StringTokenizer(line);
                task = st.nextToken();
                if(task.equals("reserve") || task.equals("search") || task.equals("delete") || task.equals("getinfo") || task.equals("done"))
                {
                    validInput =true;
                    break;
                }
                System.out.println("Invalid command.  Please enter another command or 'done' to escape.");
            }
            if(task.equals("done"))
            {
                break;
            }
            validInput = false;//reset for next line read in

            //create a new socket every time
            //Socket socket = new Socket(hostname, port);

            Socket socket = socketFactory.createSocket(hostname, port);
            din = new BufferedReader (new InputStreamReader    (socket.getInputStream()));
            pout = new PrintStream (socket.getOutputStream());

            pout.println(line);
            pout.flush();

            //print out response from server
            System.out.println(din.readLine());

        } catch (Exception e){
            System.err.println("Server aborted: " + e);
        }
    }   
}
}

最佳答案

“无法找到请求目标的有效证书路径”意味着您的信任库不信任服务器证书。将其导入您的信任库,或由公认的 CA 签名。

关于exception - SSL 服务器异常 : javax.net.ssl.SSLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9596458/

相关文章:

java - 信息 : Not binding factory to JNDI, 未配置 JNDI 名称 org.hibernate.hql.ast.QuerySyntaxException:<TableName> 未映射 [FROM <TableName>]

ruby - 引发 OpenURI::HTTPError 导致错误的参数数量错误

android - org.json.JSONException : Value of type java. lang.String 无法转换为 JSONObject

ssl - 无法通过 XAMPP 生成 SSL 证书

performance - PooledCloseableHttpClient 影响性能吞吐量

.net - 无法为 SOAP 调用的 SSL/TLS 建立安全通道

c# - C# 中 'try' 的性能成本

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

c++ - Boost、asio、https 和主机/证书验证

ssl - 将 TLS 与 mosquitto 一起使用时遇到错误