Java .等于和.一起习惯
我遇到的问题是。平等。我正在检查密码字符串是否等于数据库中的密码值。我的serlvet中有密码字符串,它从表单中获取密码。它会检查用户名,但不会检查密码Java .等于和.一起习惯,java,jpa,Java,Jpa,我遇到的问题是。平等。我正在检查密码字符串是否等于数据库中的密码值。我的serlvet中有密码字符串,它从表单中获取密码。它会检查用户名,但不会检查密码 public boolean acceptCustomer(String username, String password){ CustomerTableClass customer = new CustomerTableClass(); customer.setCustomerUsername(username); customer.set
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");
}
你似乎有一个额外的括号。试试这个:
(customerTest!=null && password.equals(customer.getPassword() ) )
您的密码字段可能是以某种形式编码的。您需要对用户在登录表单上传递的密码进行散列,然后根据从数据库获取的密码进行检查。您的数据库密码也可能有错误。如果它有salt,那么您需要将salt附加到用户传入的密码中,然后拥有它,然后将其与数据库中的密码进行比较。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");
}
}
您是否检查了getPassword方法返回的值?因为我看不出有什么错误。那么调试器说的是
password
和customer.getPassword()的值?您还执行了哪些其他诊断步骤?另外,请格式化您的代码示例。还有一个地方是数据库中以纯文本形式存储密码的地方。不要这样做。使用bcrypt。如果您要关闭entityManager,我认为您应该在try{}块的底部以及可能在finally{}子句中执行此操作