Thursday, 4 July 2013

Java Remote Method Invocation (RMI) with Secure Socket Layer (SSL)

In this post I will demonstrate how a Java Remote Method Invocation (RMI) application can make remote invocations over Secure Socket Layer (SSL) connections.

Environment

Microsoft Windows 7
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
 

Before we actually start coding the RMI Client and Server, we have to do following:

Define RMI Remote Interface

Implement RMI Remote Interface

Create RMI Client which will consume/invoke RMI method remotely

Finally, create RMI Registry

Copy ‘test-keystore’ created here and ‘test-truststore’ created here to the folder where your compiled classes reside.

Now, open up a console/command prompt and ‘cd’ to the directory where your compiled class files are, and:

Run RMIRegistry

java -Djavax.net.ssl.keyStore=test-keystore -Djavax.net.ssl.keyStorePassword=test-keystore-pwd -Djavax.net.ssl.trustStore=test-truststore -Djavax.net.ssl.trustStorePassword=test-truststore-pwd smhumayun.codeoftheday.RmiOverSslTest.RmiRegistry

Run RmiRegistry

Run CentralTimeServiceImpl

java -Djavax.net.ssl.keyStore=test-keystore -Djavax.net.ssl.keyStorePassword=test-keystore-pwd -Djavax.net.ssl.trustStore=test-truststore -Djavax.net.ssl.trustStorePassword=test-truststore-pwd smhumayun.codeoftheday.RmiOverSslTest.CentralTimeServiceImpl

Run CentralTimeServiceImpl

Run CentralTimeServiceClient

java -Djavax.net.ssl.keyStore=test-keystore -Djavax.net.ssl.keyStorePassword=test-keystore-pwd -Djavax.net.ssl.trustStore=test-truststore -Djavax.net.ssl.trustStorePassword=test-truststore-pwd smhumayun.codeoftheday.RmiOverSslTest.CentralTimeServiceClient

Run CentralTimeServiceClient

CLICK HERE TO DOWNLOAD COMPLETE SOURCE CODE