在java rmi中实现SSL

时间:2014-11-04 14:06:10

标签: java ssl rmi

我正在尝试使用Java RMI实现SSL,其中只有服务器需要证书。

我使用创建了我的证书 this guide

当我使用CMD和以下命令运行服务器和客户端时

% java -Djavax.net.ssl.keyStore=keystore \
  -Djavax.net.ssl.keyStorePassword=password Server

% java -Djavax.net.ssl.trustStore=truststore \
  -Djavax.net.ssl.trustStorePassword=trustword Client

一切都很好......但我想在我的eclipse项目中导入这些命令,所以我所做的就是服务器:

  super(0, new SslRMIClientSocketFactory(), new SslRMIServerSocketFactory());
  System.setProperty("javafx.net.ssl.keyStore","C:\\Users\\Slavi\\workspace\\rmiSSL\\keystore.jks");
  System.setProperty("javafx.net.ssl.keyStorePassword","password");    

并为客户提供:

  System.setProperty("javafx.net.ssl.trustStore","C:\\Users\\Slavi\\workspace\\rmiSSL\\truststore.jks");
  System.setProperty("javafx.net.ssl.trustStorePassword","trustword");    

当客户端连接时出现以下错误

    C:\Users\Slavi\workspace\rmiSSL>java HelloClient
Exception in thread "main" java.rmi.ConnectIOException: error during JRMP connec
tion establishment; nested exception is:
        javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_fai
lure
        at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
        at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
        at sun.rmi.server.UnicastRef.invoke(Unknown Source)
        at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unkn
own Source)
        at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
        at com.sun.proxy.$Proxy0.sayHello(Unknown Source)
        at HelloClient.main(HelloClient.java:12)
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)
        ... 7 more    

我认为问题是服务器没有在开始时设置属性并默认使用某些东西,但我不知道究竟是什么原因导致它并且我非常坚持:(

有什么想法吗?

修改

服务器代码:

public class HelloImpl extends UnicastRemoteObject implements Hello {
public HelloImpl() throws RemoteException {
    super(0, new SslRMIClientSocketFactory(), new SslRMIServerSocketFactory());
    System.setProperty("javax.net.ssl.keyStore","C:\\Users\\Slavi\\workspace\\rmiSSL\\keystore.jks");
    System.setProperty("javax.net.ssl.keyStorePassword","password");
    LocateRegistry.createRegistry(3000);
    System.out.println("RMI registry running on port 3000");
}
public String sayHello() {
    return "Hello World!";
}
public static void main(String args[]) throws Exception {

    // Get reference to the RMI registry running on port 3000 in the local host
    Registry registry = LocateRegistry.getRegistry(null, 3000);
    // Bind this object instance to the name "HelloServer"
    HelloImpl obj = new HelloImpl();
    registry.bind("HelloServer", obj);
    System.out.println("HelloServer bound in registry");
}    

和客户

public class HelloClient {
public static void main(String args[]) throws Exception {
    System.setProperty("javax.net.ssl.trustStore","C:\\Users\\Slavi\\workspace\\rmiSSL\\truststore.jks");
    System.setProperty("javax.net.ssl.trustStorePassword","trustword");
    // Get reference to the RMI registry running on port 3000 in the local host
    Registry registry = LocateRegistry.getRegistry(null, 3000);
    // Lookup the remote reference bound to the name "HelloServer"
    Hello obj = (Hello) registry.lookup("HelloServer");
    String message = obj.sayHello();
    System.out.println(message);
}   

1 个答案:

答案 0 :(得分:0)

javafx.net.ssl.keyStore

那应该是

javax.net.ssl.keyStore

其他三个相同。

您需要将这些文件放在其他位置。您的用户目录不在目标计算机上。

当然,您需要在创建依赖它们的任何远程对象之前设置这些属性