.equals和.get一起使用

时间:2013-03-29 23:22:53

标签: java jpa

我遇到的问题是.equals。我试图检查密码字符串是否等于数据库中的密码值。我的serlvet中有密码字符串,它从表单中获取密码。它检查用户名是否正常但不是密码

public boolean acceptCustomer(String username, String password){
CustomerTableClass customer = new CustomerTableClass();
customer.setCustomerUsername(username);
customer.setCustomerPassword(password);
Boolean check = null;
try
{
entityManager = entityManagerFactory.createEntityManager();             
CustomerTableClass customerTest = new CustomerTableClass();
customerTest = entityManager.find(customer.getClass(),username);
if (customerTest!=null)&&(password.equals(customer.getPassword()))){

check= true; 
System.out.println("User found");

}

3 个答案:

答案 0 :(得分:1)

你似乎有一个额外的支架。试试这个:

(customerTest!=null && password.equals(customer.getPassword() ) )

答案 1 :(得分:0)

您的密码字段可能以某种hash编码。您需要在登录表单上散列用户传入的密码,然后根据从数据库中提取的密码进行检查。您的数据库密码也可能包含salt。如果它有一个盐,那么你需要将salt附加到用户传入的密码,然后使用它,然后将其与数据库中的密码进行比较。

答案 2 :(得分:0)

customer.getPassword()未从数据库中获取密码。我必须在.equals中使用它之前找到并检索密码。它的完美工作现在是代码

    public boolean acceptCustomer(String username, String password){
    CustomerTableClass customer = new CustomerTableClass();
    customer.setCustomerUsername(username);
    customer.setCustomerPassword(password);
    Boolean check = null;

    try 
    {
        entityManager = entityManagerFactory.createEntityManager();             
        CustomerTableClass customerTest = new CustomerTableClass();
        customerTest = entityManager.find(customer.getClass(),username);// .find checks if username aleady exists 
        if (customerTest!=null ){// if user exists
            customer = entityManager.find(customer.getClass(), username);// retrieve user from db
            customer.getPassword();//retrieve password from db
            if (password.equals(customer.getPassword()))//if they are equal 
            {
                System.out.println("user and password correct");
                check= true;    
            }
            else
            {
                System.out.println("user and password not correct");
                check= false;
                entityManager.close();
            }
        }
        else 
        {
            System.out.print("user does not exist");
        }
    }