java - 通过 SSL 的 RMI : Handshake failure

标签 java ssl rmi

你好,我刚开始使用 SSL,我想做一个非常简单的例子,这里是代码:

界面。

package tpfinal;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface CentralTimeService extends Remote {

    int PORT = 8844;

    long getTime() throws RemoteException;

}

Client:

package tpfinal;

import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.rmi.ssl.SslRMIClientSocketFactory;

public class CentralTimeServiceClient {

public static void main(String[] args) {
try {
            System.setProperty("javax.net.ssl.keyStore","C:\\Users\\luciano\\workspace\\Distribuida\\bin\\keystore");
                System.setProperty("javax.net.ssl.keyStorePassword","123456");
                System.setProperty("javax.net.ssl.trustStore","C:\\Users\\luciano\\workspace\\Distribuida\\bin\\truststore");
                System.setProperty("javax.net.ssl.trustStorePassword","123456");
                Registry registry = LocateRegistry.getRegistry(null, CentralTimeService.PORT, new SslRMIClientSocketFactory());
                CentralTimeService cts = (CentralTimeService) registry.lookup("CentralTimeService");
                //Invocamos llamada RMI

long serverTime = cts.getTime();
System.out.println(serverTime);

System.out.println("Central server time is: " + SimpleDateFormat.getDateInstance().format(new Date(serverTime)));

} catch (AccessException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}

        }

    }

服务器:

打包 tpfinal;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;

public class CentralTimeServiceImpl extends UnicastRemoteObject implements CentralTimeService {

    protected CentralTimeServiceImpl() throws RemoteException{
        super(0, new SslRMIClientSocketFactory(), new SslRMIServerSocketFactory(null, null, true));
        System.setProperty("javax.net.ssl.keyStore","C:\\Users\\luciano\\workspace\\Distribuida\\bin\\keystore");
        System.setProperty("javax.net.ssl.keyStorePassword","123456");
        System.setProperty("javax.net.ssl.trustStore","C:\\Users\\luciano\\workspace\\Distribuida\\bin\\truststore");
        System.setProperty("javax.net.ssl.trustStorePassword","123456");
    }

    @Override
    public long getTime() throws RemoteException {
        return System.currentTimeMillis();
    }

    public static void main(String[] args) {
        try {
            Registry registry = LocateRegistry.getRegistry(null, CentralTimeService.PORT, new SslRMIClientSocketFactory());
            CentralTimeServiceImpl ctsi = new CentralTimeServiceImpl();
            registry.bind("CentralTimeService", ctsi);

            System.out.println("Central time service bound in registry");
        } catch (Exception e) {
            System.out.println("Central time error: " + e.getMessage());
            e.printStackTrace();
        }

    }



}

最后是 RmiRegistry:

package tpfinal;

import java.rmi.registry.LocateRegistry;

import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;

public class RmiRegistry {

    public static void main(String[] args) throws Exception {
        LocateRegistry.createRegistry(CentralTimeService.PORT,new SslRMIClientSocketFactory(), new SslRMIServerSocketFactory(null, null, true));
        System.out.println("Rmi Registry running on port " + CentralTimeService.PORT);
        System.setProperty("javax.net.ssl.keyStore","C:\\Users\\luciano\\workspace\\Distribuida\\bin\\keystore");
        System.setProperty("javax.net.ssl.keyStorePassword","123456");
        System.setProperty("javax.net.ssl.trustStore","C:\\Users\\luciano\\workspace\\Distribuida\\bin\\truststore");
        System.setProperty("javax.net.ssl.trustStorePassword","123456");
        //Sleep
        Thread.sleep(Long.MAX_VALUE);
    }

}

当我执行注册表时工作正常,但是当我执行服务器时给我这个错误,我不明白为什么。

Central time error: error during JRMP connection establishment; nested exception is: 
    javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is: 
    javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
    at sun.rmi.server.UnicastRef.newCall(Unknown Source)
    at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
    at tpfinal.CentralTimeServiceImpl.main(CentralTimeServiceImpl.java:31)
Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    at sun.security.ssl.Alerts.getSSLException(Unknown Source)
    at sun.security.ssl.Alerts.getSSLException(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
    at sun.security.ssl.AppOutputStream.write(Unknown Source)
    at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
    at java.io.BufferedOutputStream.flush(Unknown Source)
    at java.io.DataOutputStream.flush(Unknown Source)

请提供任何帮助,在此先感谢。

最佳答案

  1. 您需要将所有 System.setProperty() 调用移动到注册表类中,以便在创建 Registry 之前执行它们。

  2. 需要将服务端所有的System.setProperty()调用都移到main()方法中,在获取之前执行>注册表对象。

关于java - 通过 SSL 的 RMI : Handshake failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44148542/

相关文章:

docker - 在 Kubernetes digital ocean 负载均衡器上安装 ssl

ssl - tomcat获取所有信任证书

c# - 如何撤销 WCF 中的客户端证书?

java - GWT 2.0 RC1 和 GXT 2.0.1

java - 有没有一种方法可以使用 Java 中的 Timer 类平滑图形组件从一个点到另一个点的过渡?

java - 具有泛型的 gson 的正确语法

java - 在 java rmi 中实现 SSL

java - 我如何从Android中的毫秒数中获取一天中的小时数

java.rmi.ConnectException : Connection refused to host: localhost

java - 具有多个主机的 RMI