当前位置: 首页 > 知识库问答 >
问题:

将Java SSL服务器连接到Java SSL服务器[重复]

刁浩言
2023-03-14

这就是第一台服务器的样子:

public HostServer() throws IOException {

        System.setProperty("javax.net.ssl.keyStore", HOST_SERVER_KEY_FILE);
        System.setProperty("javax.net.ssl.keyStorePassword", SSL_KEY_PASSWORD);
        serverSocket = ((SSLServerSocketFactory) SSLServerSocketFactory.getDefault()).createServerSocket(HOST_SERVER_PORT);
        System.out.println("Host server is running and waiting for clients to connect...");
        connectedRequestServers = new ArrayList<ClientData>();
        connectedRequestServersSemaphore = new Semaphore(1);


}

public void start() {

    try {
        while (true) {
            Socket socket = serverSocket.accept();
            Thread clientHandler = new Thread(new ClientHandler(socket));
            clientHandler.start();

        }
    } catch (IOException ex) {
        Logger.getLogger(HostServer.class.getName()).log(Level.SEVERE, null, ex);
    }

}

这是第二台服务器的样子:

  public RequstServer() throws IOException, NoSuchAlgorithmException, KeyManagementException, GeneralSecurityException {



        System.setProperty("javax.net.ssl.keyStore", HostServer.HOST_SERVER_KEY_FILE);// REQUST_SERVER_KEY_FILE);
        System.setProperty("javax.net.ssl.keyStorePassword", HostServer.SSL_KEY_PASSWORD); //SSL_KEY_PASSWORD);
        serverSocket = ((SSLServerSocketFactory) SSLServerSocketFactory.getDefault()).createServerSocket(REQUEST_SERVER_PORT);

        System.out.println("Request Server is up and running!");

        System.setProperty("javax.net.ssl.trustStore", HostServer.HOST_SERVER_KEY_FILE);

        hostSocket = ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(HostServer.HOST_SERVER_ADDRESS, HostServer.HOST_SERVER_PORT);
        os = new ObjectOutputStream(hostSocket.getOutputStream());
        is = new ObjectInputStream(hostSocket.getInputStream());


    }

第一台服务器运行良好,但当我试图运行第二台服务器时,我会得到follwing错误,这意味着到第一台服务器的连接失败。如果有人能帮助我,我会很高兴的!

Request Server is up and running!

3:12:47 PM Ver1.RequstServer main
SEVERE: null
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
    at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:747)
    at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
    at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
    at java.io.ObjectOutputStream.<init>(ObjectOutputStream.java:247)
    at Ver1.RequstServer.<init>(RequstServer.java:70)
    at Ver1.RequstServer.main(RequstServer.java:38)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
    ... 12 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
    ... 18 more

共有1个答案

司空和悌
2023-03-14

服务器端:

System.setProperty("javax.net.ssl.trustStore", keyStore);
System.setProperty("javax.net.ssl.keyStore", keyStore);
System.setProperty("javax.net.ssl.keyStorePassword", keyStorePass);

SSLServerSocketFactory sslFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
serverSocket = (SSLServerSocket) sslFactory.createServerSocket(port, queueLength);

Socket clientConnection = socket.accept();

客户端:

System.setProperty("javax.net.ssl.trustStore", keyStore);
System.setProperty("javax.net.ssl.keyStore", keyStore);
System.setProperty("javax.net.ssl.keyStorePassword", keyStorePass);

SSLSocketFactory sslFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
socket = (SSLSocket) sslFactory.createSocket(address, port);
socket.startHandshake();

要生成密钥存储库,请执行以下操作:

keytool -genkey -alias server -keyalg RSA -keystore server.jks
keytool -genkey -alias client -keyalg RSA -keystore client.jks

keytool -export -file server.cert -keystore server.jks -storepass 123456 -alias server
keytool -export -file client.cert -keystore client.jks -storepass 123456 -alias client

keytool -import -file client.cert -keystore server.jks -storepass 123456 -alias client
keytool -import -file server.cert -keystore client.jks -storepass 123456 -alias server
 类似资料:
  • 我使用Sabre DAV在PHP中实现了一个webdav目录,用于我的网站(Application Server Webinterface)。 对于这个网站,我现在使用C#编写了一个TCP套接字,它运行在另一个服务器上(实际上它在同一个数据中心,但从理论上讲,它在另一个半球上)。 我想连接我的网络Dav到我的套接字的FTP服务器,这意味着文件监听,下载,上传。用户只能连接到一个服务。想象一下,我的

  • 我正试图通过AnyLogic 7.3.6连接到本地SQL数据库。我安装了SQL Server 2016 Developer,数据库采用混合身份验证(Windows) 网络错误IOException:连接被拒绝:连接 拒绝连接:连接 我的主机名为。我曾尝试将登录名和密码留空,希望Windows身份验证,并尝试将SQL用户(Test,Test)添加到服务器,两者的结果相同。我正在使用JDBC驱动程序,

  • 我正在尝试连接两个docker容器,一个是posgresql,另一个是python flask应用程序。两者都链接正确,python应用程序中的所有连接变量都直接取自postgres容器中通过链接公开的连接变量,并且与检查postgresql容器时发现的连接变量相同。当我将psql与连接字符串中的精确参数一起使用时,即: 成功连接到postgres容器中的数据库,因此我知道postgres正在通过

  • 我已经在Ubuntu14.04上安装并启动了nginx服务器。我的目标是使用HLS(http live streaming)流媒体视频(live)。我遵循了本教程https://www.vultr.com/docs/setup-nginx-on-ubuntu-to-stream-live-hls-video,它推荐使用OBS-Studio。然而,我不知道如何从OBS-STUDIO流到Nginx,然

  • 我有一个nodejs聊天服务器,它有不同的事件“Connection”,“SendChat”,“Switchroom”... null Thx很多!

  • 在我的C#项目中,为了连接到SFTP服务器,我包含了。目前,我尝试实现一个简单的连接方法,如下所示: 已连接,但无法接收来自FTP服务器的问候语。 根据Chilkat的文件。 我错过了什么?我尝试连接到一个简单的ftp服务器测试(没有SSL/TLS),它连接正确,所以我想我错过了什么。当我尝试用Filezilla连接到SFTP服务器时,给出的凭据是正确的。谢谢你。 更新