In: Computer Science
URGENT JAVA
Develop a Java computer program to simulate access control to a system network. The username and password authentication mechanism is used as an access control mechanism in this project. The program needs to authenticate the user with their username and hashed password stored in Access Manager User (AMU) file. To create an Access Manager User file, you need to develop a java program that takes username and password as input and then generates a file containing the username, hashed password, and plaintext password. In real world, the plaintext are not saved in the AMU file. However, you need the username and corresponding plaintext password for testing of your main program. The structure of the AMU file is shown in the appendix. Note that this should be done as a separate program. The main program operates in the following manner. It first prompts the user to enter username and password. After the user enters both username and password, it hashes the password and search the AMU file to verify both the user name and password. If the program verifies the username and password, it returns the message “Access Granted.” If the programcould not verify the username, it returns the message “Access Denied. You are not a registered user.” If the program verifies the username but not the password, it prompts the user to enter the password again. If the user enter the password three times incorrectly, the system locks the user account and gives this message. “You exceeded entering the allowablenumber of incorrect password. Your account is locked. Contact system administrator to unlock your account”
import java.io.*;
import java.util.Scanner;
public class loginValidation {
public static void main(String[] args) {
// To add credentials call as below
// addCredentials("userName", "Password");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Username:");
String useerName;
useerName = reader.readLine();
System.out.println("Enter Password:");
String password = reader.readLine();
System.out.println(validateUserCredentials(useerName, password));
} catch (IOException e) {
e.printStackTrace();
}
}
public static String validateUserCredentials(String userName, String password) {
try {
File myObj = new File("amu.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
String[] userData = data.split(";");
if(userData[0].equals(userName)) {
if(userData[1].equals(String.valueOf(password.hashCode()))) {
return "Access Granted";
}
else {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int i = 0;
while (!userData[1].equals(String.valueOf(password.hashCode())) && i < 3) {
System.out.println("Incorrect Password, Enter Again: ");
password = reader.readLine();
i++;
}
if( i == 3) {
return "You excedeed entering the allowiable no.of incorrect password, your account is locked. contact system administrator to unlock account";
}
else {
return "Access Granted";
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
return "Access Denied. You are not a registered user";
}
public static void addCredentials(String userName, String password) {
try
{
File myObj = new File("amu.txt");
if (myObj.createNewFile()) {
System.out.println("File created: " + myObj.getName());
}
String writeData = userName + ";" + password.hashCode() + ";" + password + "\n";
BufferedWriter out = new BufferedWriter(new FileWriter("amu.txt", true));
out.write(writeData);
out.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}