Simple Login Application in Java RMI
We are attaching Simple program about, How to write Simple Login Application in Java RMI. The example were tested and output has been shared in the same post.
Authentication.java
package com.dineshkrish.rmi; import java.rmi.Remote; import java.rmi.RemoteException; public interface Authentication extends Remote { public boolean authenticate(String userName, String password) throws RemoteException ; }
AuthenticationImpl.java
package com.dineshkrish.rmi; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class AuthenticationImpl extends UnicastRemoteObject implements Authentication { protected AuthenticationImpl() throws RemoteException { } @Override public boolean authenticate(String userName, String password) throws RemoteException { if ((userName != null && !userName.isEmpty()) && (password != null && !password.isEmpty())) { if((userName.equalsIgnoreCase("admin")) && (password.equalsIgnoreCase("admin"))) { return true; } } return false; } }
Server.java
package com.dineshkrish.rmi; import java.rmi.AlreadyBoundException; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class Server { // Port Number you can change based on your system availability private static final int PORT = 5252; public static void main(String[] args) { try { // Defining Object Authentication authentication = new AuthenticationImpl(); // Creating RMI Registry with Port Registry registry = LocateRegistry.createRegistry(PORT); // Binding the Object registry.bind("auth", authentication); System.out.println("Authentication Service running at "+PORT+" port..."); } catch (RemoteException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch (AlreadyBoundException e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
ApplicationClient.java
package com.dineshkrish.rmi; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import java.util.Scanner; public class ApplicationClient { public static void main(String[] args) { Authentication authentication = null; try { // Getting Object using Remote Address authentication = (Authentication)Naming.lookup("rmi://localhost:5252/auth"); Scanner scanner = new Scanner(System.in); System.out.println("Enter the username : "); String userName = scanner.next(); System.out.println("Enter the password : "); String password = scanner.next(); // Invoking the Method boolean status = authentication.authenticate(userName, password); if(status) { System.out.println("You are an authorized user..."); // Do Something.... } else { System.out.println("Unauthorized Login Attempt"); } scanner.close(); } catch (MalformedURLException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch (RemoteException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch (NotBoundException e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Running Server Program
Authentication Service running at 5252 port…
Running Client Program
Enter the username : admin
Enter the password : admin
You are an authorized user…
Download Source Code
References
1. Java RMI API JavaDocs
2. How to Create Simple RMI Application using Java
More from my site
Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.