试图完成一个java代码程序
我正在为学校编写一个java代码,我已经花了好几天的时间在上面,我只是觉得我的方向不对。以下是有关该项目的信息: 对于有安全意识的专业人士来说,只有合适的人才能访问计算机系统中的数据,这一点很重要。这称为身份验证。一旦用户进入,他们只看到与他们在计算机系统中的角色相关的数据也很重要。这称为授权。对于zoo,您将开发一个身份验证系统来管理身份验证和授权。您已获得一个凭据文件,其中包含授权用户的凭据信息。您还获得了三个文件,每个角色一个:动物园管理员、兽医和管理员。每个角色文件描述特定角色应有权访问的数据。创建执行以下所有操作的身份验证系统: 要求用户输入用户名 要求用户输入密码 使用消息摘要五(MD5)哈希转换密码 不需要从头开始编写MD5。使用本文档中的代码并按照其中的注释执行此操作。 根据凭据文件中提供的有效凭据检查凭据 使用第二列中的哈希密码;第三列包含用于测试的实际密码,第四行包含 每个用户的角色。 在通知用户并退出程序之前,将失败的尝试限制为三次 在成功进行身份验证后,为经过身份验证的用户提供对正确角色文件的访问权限 应显示存储在角色文件中的系统信息。例如,如果zookeeper的凭据已成功通过身份验证,则将显示zookeeper文件中的内容。如果管理员的凭据已成功通过身份验证,则将显示管理员文件中的内容。 允许用户注销 停留在凭证屏幕上,直到尝试成功、尝试三次失败或用户选择退出 以下是我收到的五个文本文件: admin.txt 你好,系统管理员 作为管理员,您可以访问动物园的主计算机系统。试图完成一个java代码程序,java,Java,我正在为学校编写一个java代码,我已经花了好几天的时间在上面,我只是觉得我的方向不对。以下是有关该项目的信息: 对于有安全意识的专业人士来说,只有合适的人才能访问计算机系统中的数据,这一点很重要。这称为身份验证。一旦用户进入,他们只看到与他们在计算机系统中的角色相关的数据也很重要。这称为授权。对于zoo,您将开发一个身份验证系统来管理身份验证和授权。您已获得一个凭据文件,其中包含授权用户的凭据信息。您还获得了三个文件,每个角色一个:动物园管理员、兽医和管理员。每个角色文件描述特定角色应有权访问
这允许您监视系统中的用户及其角色 credentials.txt
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package authentication;
import java.security.MessageDigest;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
/**
*
* @author JoeP
*/
public class ValidateCredentials {
private boolean isValid;
private String filePath;
private String credentialsFileName;
public ValidateCredentials() {
isValid = false;
//filePath = "C:\\Users\\joep\\Documents\\NetBeansProjects\\ Authentication\\";
filePath = "";
credentialsFileName = "credentials";
}
public boolean isCredentialsValid(String userName, String passWord) throws Exception {
String original = passWord;
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(original.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
System.out.println("");
System.out.println("original:" + original);
System.out.println("digested:" + sb.toString()); //sb.toString() is what you'll need to compare password strings
isValid = readDataFiles(userName, sb.toString());
return isValid;
}
public boolean readDataFiles(String userName, String passWord) throws IOException {
FileInputStream fileByteStream1 = null; // File input stream
FileInputStream fileByteStream2 = null; // File input stream
Scanner inFS1 = null; // Scanner object
Scanner inFS2 = null; // Scanner object
String textLine = null;
boolean foundCredentials = false;
// Try to open file
System.out.println("");
System.out.println("Opening file " + credentialsFileName + ".txt");
fileByteStream1 = new FileInputStream(filePath + "credentials.txt");
inFS1 = new Scanner(fileByteStream1);
System.out.println("");
System.out.println("Reading lines of text.");
while (inFS1.hasNextLine()) {
textLine = inFS1.nextLine();
System.out.println(textLine);
if (textLine.contains(userName) && textLine.contains(passWord)) {
foundCredentials = true;
break;
}
}
// Done with file, so try to close it
System.out.println("");
System.out.println("Closing file " + credentialsFileName + ".txt");
if (textLine != null) {
fileByteStream1.close(); // close() may throw IOException if fails
}
if (foundCredentials == true) {
// Try to open file
System.out.println("");
System.out.println("Opening file " + userName + ".txt");
fileByteStream2 = new FileInputStream(filePath + userName + ".txt");
inFS2 = new Scanner(fileByteStream2);
System.out.println("");
while (inFS2.hasNextLine()) {
textLine = inFS2.nextLine();
System.out.println(textLine);
}
// Done with file, so try to close it
System.out.println("");
System.out.println("Closing file " + userName + ".txt");
if (textLine != null) {
fileByteStream2.close(); // close() may throw IOException if fails
}
}
return foundCredentials;
}
}
格里芬·凯斯108DE81C31BF9C622F768766B74E9285F“字母汤”动物园管理员
rosario.dawson 3E34BAA4 EE2FF767AF8C120A496742B5“动物医生”管理员
伯尼大猩猩a584efafa8f9ea7fe5cf18442f32b07b“秘密密码”兽医
donald.monkey 17B1B7D8A7066966ED220BC414F729AD3“M0nk3y业务”动物园管理员
jerome.grizzlybear 3ADE92111E6307F8F2AAE4721E77900“灰熊1234”兽医
bruce.grizzlybear 0d107d09f5bbe40cade3de5c71e9e9b7“letmein”管理员
validateCredentials.txt
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package authentication;
import java.security.MessageDigest;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
/**
*
* @author JoeP
*/
public class ValidateCredentials {
private boolean isValid;
private String filePath;
private String credentialsFileName;
public ValidateCredentials() {
isValid = false;
//filePath = "C:\\Users\\joep\\Documents\\NetBeansProjects\\ Authentication\\";
filePath = "";
credentialsFileName = "credentials";
}
public boolean isCredentialsValid(String userName, String passWord) throws Exception {
String original = passWord;
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(original.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
System.out.println("");
System.out.println("original:" + original);
System.out.println("digested:" + sb.toString()); //sb.toString() is what you'll need to compare password strings
isValid = readDataFiles(userName, sb.toString());
return isValid;
}
public boolean readDataFiles(String userName, String passWord) throws IOException {
FileInputStream fileByteStream1 = null; // File input stream
FileInputStream fileByteStream2 = null; // File input stream
Scanner inFS1 = null; // Scanner object
Scanner inFS2 = null; // Scanner object
String textLine = null;
boolean foundCredentials = false;
// Try to open file
System.out.println("");
System.out.println("Opening file " + credentialsFileName + ".txt");
fileByteStream1 = new FileInputStream(filePath + "credentials.txt");
inFS1 = new Scanner(fileByteStream1);
System.out.println("");
System.out.println("Reading lines of text.");
while (inFS1.hasNextLine()) {
textLine = inFS1.nextLine();
System.out.println(textLine);
if (textLine.contains(userName) && textLine.contains(passWord)) {
foundCredentials = true;
break;
}
}
// Done with file, so try to close it
System.out.println("");
System.out.println("Closing file " + credentialsFileName + ".txt");
if (textLine != null) {
fileByteStream1.close(); // close() may throw IOException if fails
}
if (foundCredentials == true) {
// Try to open file
System.out.println("");
System.out.println("Opening file " + userName + ".txt");
fileByteStream2 = new FileInputStream(filePath + userName + ".txt");
inFS2 = new Scanner(fileByteStream2);
System.out.println("");
while (inFS2.hasNextLine()) {
textLine = inFS2.nextLine();
System.out.println(textLine);
}
// Done with file, so try to close it
System.out.println("");
System.out.println("Closing file " + userName + ".txt");
if (textLine != null) {
fileByteStream2.close(); // close() may throw IOException if fails
}
}
return foundCredentials;
}
}
兽医.txt
你好,兽医
作为兽医,你可以查阅所有动物的健康记录。这允许您查看每只动物的病史、当前治疗/疾病(如果有),并维护疫苗接种日志
zookeeper.txt
喂,动物园管理员
作为动物园管理员,您可以访问所有动物信息及其日常监测日志。这可以让你追踪它们的食性、栖息地条件和一般福利
最后,这是到目前为止我所拥有的代码,但当我尝试在Netbeans中运行它时,它就无法工作
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.util.List;
import java.util.Scanner;
public class Authentication {
public static void main(String args[]) {
try {
Scanner scan = null;
scan = new Scanner(new File("credentials.txt"));
String credentials[][] = new String[100][4];
int count = 0;
while (scan.hasNextLine()) {
//read name and hased pass
credentials[count][0] = scan.next();
credentials[count][1] = scan.next();
//get original pass from file
String l[] = scan.nextLine().split("\"[ ]+");
l[0] = l[0].trim();
l[0] = l[0].replace("\"", "");
credentials[count][2] = l[0];
credentials[count][3] = l[1].trim();
count++;
}
//ask for user input
Scanner scanio = new Scanner(System.in);
boolean RUN = true;
int tries = 0;
while (RUN) {
System.out.println("**WELCOME**");
System.out.println("1) Login");
System.out.println("2) Exit");
int ch = Integer.parseInt(scanio.nextLine().trim());
if (ch == 1) {
//increment number of attempts
tries++;
//ask for user and pass
System.out.print("Input username:");
String username = scanio.nextLine();
System.out.print("Input password:");
String password = scanio.nextLine();
//generate hash
MessageDigest md;
md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
byte[] digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
String hPassword = sb.toString();
boolean badUser = true;
for (int i = 0; i < count; i++) {
if (username.contentEquals(credentials[i][0])) {
if (hPassword.contentEquals(credentials[i][1])) {
//everything looks good. login
List<String> data = null;
//check type of user and print
switch (credentials[i][3]) {
case "zookeeper":
data = Files.readAllLines(Paths.get("zookeeper.txt"), Charset.defaultCharset());
break;
case "admin":
data = Files.readAllLines(Paths.get("admin.txt"), Charset.defaultCharset());
break;
case "veterinarian":
data = Files.readAllLines(Paths.get("veterinarian.txt"), Charset.defaultCharset());
break;
default:
break;
}
for (String s : data) {
System.out.println(s);
}
//reset tries
tries = 0;
//now what to do?
System.out.println("\n1) Logout.");
System.out.println("2) Exit.");
ch = Integer.parseInt(scanio.nextLine().trim());
if (ch == 2) {
RUN = false;
}
badUser = false;
break;
}
}
}
if (badUser) {
System.out.println("Invalid Username or password.");
}
} else {
RUN = false;
break;
}
//thief alert!!
if (tries == 3) {
RUN = false;
System.out.println("Too many invlaid attempts.");
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
导入java.io.File;
导入java.nio.charset.charset;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.security.MessageDigest;
导入java.util.List;
导入java.util.Scanner;
公共类身份验证{
公共静态void main(字符串参数[]){
试一试{
扫描仪扫描=空;
扫描=新扫描仪(新文件(“credentials.txt”);
字符串凭据[][]=新字符串[100][4];
整数计数=0;
while(scan.hasNextLine()){
//读名字并通过
凭据[count][0]=scan.next();
凭证[count][1]=扫描.next();
//从文件中获取原始通行证
字符串l[]=scan.nextLine().split(“\”[]+”);
l[0]=l[0].trim();
l[0]=l[0]。替换(“\”,“);
凭证[计数][2]=l[0];
凭证[count][3]=l[1].trim();
计数++;
}
//请求用户输入
Scanner scanio=新扫描仪(System.in);
布尔运行=真;
int=0;
while(运行){
System.out.println(“**欢迎**”);
System.out.println(“1)登录);
系统输出打印(“2)退出”);
int ch=Integer.parseInt(scanio.nextLine().trim());
如果(ch==1){
//增加尝试次数
尝试++;
//询问用户和通行证
系统输出打印(“输入用户名:”);
字符串username=scanio.nextLine();
系统输出打印(“输入密码:”);
字符串密码=scanio.nextLine();
//生成散列
信息文摘md;
md=MessageDigest.getInstance(“MD5”);
md.update(password.getBytes());
字节[]摘要=md.digest();
StringBuilder sb=新的StringBuilder();
for(字节b:摘要){
sb.append(字符串格式(“%02x”,b&0xff));
}
字符串hPassword=sb.toString();
布尔badUser=true;
for(int i=0;i