使用 HashMap 读取数据后如何检查凭据是否属于该特定用户?
After Reading the data using HashMap how to check if the credentials are of that particular user?
因此,我正在尝试创建一个登录名,其中的数据位于一个文件中,并且该数据已通过哈希映射读取。那么如何检查它是否是该特定用户的电子邮件和密码并授予访问权限?
private void readLoginData(String Email, char[] pwd) throws IOException {
String loginEmail = LoginPanel.getLoginEmail().getText().trim();
char[] loginPassword = LoginPanel.getLoginPassword().getPassword();
HashMap<String, String> hash_map = new HashMap<>();
BufferedReader read = new BufferedReader(new FileReader(file + "\register.txt"));
String line;
while ((line = read.readLine()) != null)
{
String[] split = line.split(": ", 2);
if (split.length >= 2)
{
String key = split[0];
String value = split[1];
hash_map.put(key, value);
hash_map.remove("First Name");
hash_map.remove("Last Name");
hash_map.remove("User Type");
}
}
read.close();
}
在 read.close() 之后,您可以检查电子邮件密钥的 hashmap 值是否与密码匹配:
String loginPasswordAsString = String.valueOf(loginPassword); // convert the loginPassword from char[] to String
boolean match = false;
if (hash_map.containsKey(loginEmail)) {
String passwordForEmail = hash_map.get(loginEmail); // get the hash_map value that is associated to the key loginEmail -> this should be the password that belongs to the email
match = loginPasswordAsString.equals(passwordForEmail);
}
因此,我正在尝试创建一个登录名,其中的数据位于一个文件中,并且该数据已通过哈希映射读取。那么如何检查它是否是该特定用户的电子邮件和密码并授予访问权限?
private void readLoginData(String Email, char[] pwd) throws IOException {
String loginEmail = LoginPanel.getLoginEmail().getText().trim();
char[] loginPassword = LoginPanel.getLoginPassword().getPassword();
HashMap<String, String> hash_map = new HashMap<>();
BufferedReader read = new BufferedReader(new FileReader(file + "\register.txt"));
String line;
while ((line = read.readLine()) != null)
{
String[] split = line.split(": ", 2);
if (split.length >= 2)
{
String key = split[0];
String value = split[1];
hash_map.put(key, value);
hash_map.remove("First Name");
hash_map.remove("Last Name");
hash_map.remove("User Type");
}
}
read.close();
}
在 read.close() 之后,您可以检查电子邮件密钥的 hashmap 值是否与密码匹配:
String loginPasswordAsString = String.valueOf(loginPassword); // convert the loginPassword from char[] to String
boolean match = false;
if (hash_map.containsKey(loginEmail)) {
String passwordForEmail = hash_map.get(loginEmail); // get the hash_map value that is associated to the key loginEmail -> this should be the password that belongs to the email
match = loginPasswordAsString.equals(passwordForEmail);
}