简体   繁体   English

Java比较用户名和密码和文本文件

[英]Java-Compare username and password with text File

Currently I am working on a program that required to add multiple user and each user will have their own password, so all the username and password will save on a same text file. 目前,我正在开发一个程序,该程序需要添加多个用户,并且每个用户将拥有自己的密码,因此所有用户名和密码都将保存在同一文本文件中。 My question is, is there any solution to just comparing the username and password and that it. 我的问题是,有什么解决方案可以只比较用户名和密码。

For example, User A login, after the user type in the Username and Password it will go through the text file and take out User A username and password without touching User B or C Username and password of user B or C 例如,用户A登录,在用户键入用户名和密码后,它将通过文本文件并取出用户A的用户名和密码,而无需触摸用户B或C。用户B或C的用户名和密码

The easiest way to do this is using Properties . 最简单的方法是使用Properties A Java-Properties-File look like this: Java属性文件如下所示:

# this is a comment
name=value
user=password

I would recommend to not store the password as plain-text in your file. 我建议不要将密码以纯文本格式存储在文件中。 Saving it's hash will be enough to compare passwords: 保存它的哈希值就足以比较密码:

public class YourPasswordStorage
{
    Properties users = new Properties();

    public YourPasswordStorage (File file)
    {
       user.load(file);
    }

    void insertUser (String user, String password)
    {
        users.put(user,
            Base64.getEncoder().encodeToString(
                MessageDigest.getInstance("sha-512").digest(password)));
    }

    boolean checkUser (String user, String password)
    {
        String hash = Base64.getEncoder().encodeToString(
            MessageDigest.getInstance("sha-512").digest(password));
        return (hash.equals(users.get(user)); // note that users.get(user) could be null
    }
}


Note that the Code above won't compile because of some uncaught Exceptions, but I hope you understand how it works. 请注意,由于某些未捕获的异常,上面的代码无法编译,但是我希望您了解它是如何工作的。

Storing raw passwords in a text file isn't very safe for your users. 对于用户而言,将原始密码存储在文本文件中并不是很安全。 It's a lot better to salt your passwords first - systematically convert them into hash that you write to file. 最好先给密码加盐-将密码系统地转换为您写入文件的哈希。 Then instead of comparing password to password, you take salt the password the user gives you and compare that to what's written to file. 然后,您无需再将密码与密码进行比较,而应该使用用户提供的密码,然后将其与写入文件的密码进行比较。 Take a look here (and many other places) for how to do that. 在这里 (以及许多其他地方)看看如何做。

Have you heard NEVER GIVE A MONKEY TO DO MAN'S WORK 你有没有听说过给人做事的猴子

Well AFAICS you will have the fields username password and user so why dont use a database instead of a file. 好吧,AFAICS,您将拥有username password and user字段username password and user所以为什么不使用database而不是文件。 Take advantage of H2 database allows you to store data as file. 利用H2 database ,您可以将数据存储为文件。 Just include the driver jar and write this simple codes 只需包含driver jar并编写以下简单代码

Connection con = null;
try{
   Class.forName("org.h2.Driver");
   con = DriverManager.getConnection("jdbc:h2:./mydb;MODE=MySQL;", "", "");
   PreparedStatement stmt= con.prepareStatement
            ("INSERT INTO  yourTable(user,username,password) VALUES(?,?,?)");
   stmt.setString(1, "someUser");
   stmt.setString(2, "someUsername");
   stmt.setString(3, "somePassword");
   stmt.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
finally{
//close statements
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM