简体   繁体   English

在Java RMI中实现SSL

[英]Implementing SSL in java rmi

I am trying to implement SSL using Java RMI, where only the server needs to certificate. 我正在尝试使用Java RMI实现SSL,其中仅服务器需要进行认证。

I created my certificate using this guide ! 我使用此指南创建了证书!

when I run the server and the client using CMD and the following commands 当我使用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

everything works great .. but I want to import those commands inside my eclipse project, so what I do is for the server: 一切正常..但是我想将这些命令导入我的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");    

and for the client: 对于客户:

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

and I get the following errors when the client connects 当客户端连接时出现以下错误

    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    

I think the problem is that the server doesn't set the property at the beggining and using something by default but I have no idea what actually the reason for this is and I am pretty stuck it :( 我认为问题在于服务器没有在默认情况下开始设置和使用某些属性,但我不知道这实际上是什么原因,我很固执:(

Any ideas? 有任何想法吗?

EDIT 编辑

Server code : 服务器代码:

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

and the Client 和客户

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);
}   
javafx.net.ssl.keyStore

That should be 那应该是

javax.net.ssl.keyStore

Same for the other three. 其他三个相同。

You need to put these files elsewhere. 您需要将这些文件放在其他位置。 Your user directory won't be there on the target machines. 您的用户目录将不在目标计算机上。

And of course you need to set these properties before creating any remote objects that rely on them. 当然, 创建任何依赖它们的远程对象之前 ,您需要设置这些属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM