verifying username and password saved in a txt file Java

13,894

Solution 1

This ...

if (NameTextField.getText().contentEquals(record))
    isFound = true;
if (jPasswordField.getText().contentEquals(record))
    isFound = true;

Means that if you find either the user name OR the password in the text file, it will return true. I'm pretty sure you don't want to do this.

Better to use two flags (foundUser and foundPassword for example) which you can set as required and then return foundUser && foundPassword

Updated from feedback

When checking the username/password values, you need to make sure that the password you are trying to match is being matched against the user, something like...

if (foundUser && jPasswordField.getText().contentEquals(record)) {
    foundPassword = true;
    break; // No need to compare any more details...
} else {
    foundUser = false;
}
if (NameTextField.getText().contentEquals(record)) {
    foundUser = true;
}

Updated

As a side note, you really shouldn't get using JPasswordField#getText as it is a security risk. You should be using JPassword#getPassword and using Arrays.equals(char[], char[]) to compare them.

This, obviously raises issues with how you read the data from the text file as well.

(Mind you, having the username/passwords in a text file is probably a larger security risk :P)

Solution 2

As pointed out in other answers there are many issues you should consider:

  1. you should not store plain passwords. Compute a hash from the password and store the hash.

  2. if possible use a database instead of a text file

  3. if you cannot use a database store the username/password information on a single line of text. Here is a format which is similar to the one used in Unix password files:

    joe:fjckdsoij48738423
    marian:fjccoekrnvn3iernci3en
    mrbean:fjd84jfn4u48fu4nf
    

when you want to check a password (the hash of the password in view of point 1) you can read the password file line by line. Split the line on the occurence of the first ':' character to find username and password. Compare with user provided data.

Share:
13,894
user2001913
Author by

user2001913

Updated on July 20, 2022

Comments

  • user2001913
    user2001913 almost 2 years

    When someone registers, the username, and password is saved in a .txt file. The code below verifies each line to check if the username and password exist in the file.

    However, I don't want it to work this way because it gets a username with a random password... I need it to verify both username and password for each user registered, any help? (I'm using NetBeans)

    public boolean isCustomer(){
        boolean isFound = false;
        String record = null;
        FileReader in = null;
        try{
            in = new FileReader ("login.txt");
            BufferedReader bin = new BufferedReader(in);
            record = new String();
            while ((record = bin.readLine()) != null)
            {
                if (NameTextField.getText().contentEquals(record))
                    isFound = true;
                if (jPasswordField.getText().contentEquals(record))
                    isFound = true;
            }
    
        bin.close();
        bin = null;
     }catch(IOException ioe){
        NameTextField.setText("IOProblem");
     }   
      return isFound;
    }
    

    The login.txt looks like this (username, then password and "$$$" as a separator):

    Joe
    
    656451asda
    
    $$$
    
    Robert
    
    123456hbb
    
    $$$
    
    Tracey
    
    56464999abc
    
    $$$
    
    Celia
    
    abc1234897
    
    $$$
    
  • sdasdadas
    sdasdadas over 11 years
    That solution, though it works, skirts the logic error in the code and isn't much more than a kludge.
  • Emanuele Paolini
    Emanuele Paolini over 11 years
    But even with this correction you are checking the username among all usernames and the password among all password. But you are not checking that they correspond each other
  • MadProgrammer
    MadProgrammer over 11 years
    @manu-fatto I hope the update fixes it - Thanks for the catch!
  • user2001913
    user2001913 over 11 years
    thanks you so much! I got it working, needs a few tweaks now but excellent help! :D