mercredi 12 décembre 2018

how to use Propertiesfor user login with several user accounts on java

I have this:

propiedades = new Properties();

try {
  entrada = new FileInputStream("config.properties");
  propiedades.load(entrada);
  Set set =propiedades.stringPropertyNames();
  System.out.println(set);      
} catch (IOException ex) {
  ex.printStackTrace();
} finally {
  if (entrada != null) {
    try {
      entrada.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

So basiclly whats inside "config.properties" is the following thing:

admin=admin
user=userpass
user1:userpas1

Next up,i have this code:

    public  boolean revisarCredenciales(String userName,String password)
  {
    Enumeration<?> e = propiedades.propertyNames();
    while(e.hasMoreElements())
    {
      for (; e.hasMoreElements();) {
        System.out.println(e.nextElement());
        if (e.nextElement().equals(userName) && propiedades.getProperty(userName).equals(password))
        {
          return true;
        }
      }      
    }
    return false;
  }

In this block i tried to do a simple if where if the e.nextElement() equals userName(userName is just a txUserBox.getText()) and propiedades.getProperty(userName).equals(password(txPassword.getPassword()) then it returns a value, can be either false or true, and the method where it is called will access the program if true.

The problem comes where it always is returns "true" and with this, doesnt matter what i put on the textboxes it will log me on..

Aucun commentaire:

Enregistrer un commentaire