【发布时间】:2019-07-24 12:39:19
【问题描述】:
我正在尝试使用 Spring Boot 应用程序创建登录概念,所以我只需要 mysql 表的行数,我尝试了各种互联网答案,但没有找到解决方案,请您的帮助和建议是适当的。我将在下面提到我的代码。
控制器
@GetMapping("/Login")
public String Login(Model model) {
model.addAttribute("customer", new Customer());
return "login";
}
@PostMapping("/LoginProcess")
public String LoginProcess(@ModelAttribute("customer") Customer thecustomer,HttpSession session) {
System.out.println(thecustomer);
Customer result = customerservice.Login_service(thecustomer.getUserName(),thecustomer.getPassword());
if(result==null)
{
return "login";
}
else if(result.getRole().equals("1"))
{
return "admindash";
}
else
{
//session.setAttribute("jsp_uname", result.getUserName());
return "customerdash";
}
}
CustomerImplDao
import javax.persistence.EntityManager;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.example.filedemo.model.Customer;
@Repository
public class CustomerDaoImpl implements Customerdao {
@Autowired
private EntityManager entityManager;
@Override
public void save(Customer theCustomer) {
Session cursession = entityManager.unwrap(Session.class);
//If Id=0 then It will do insert or id=x then it will do update
cursession.save(theCustomer);
}
@Override
public Customer Login(String username,String password) {
Session cursession = entityManager.unwrap(Session.class);
String hql="from Customer c where c.UserName=:username and c.password=:password";
Query<Customer> query = cursession.createQuery(hql,Customer.class);
query.setParameter("username", username);
query.setParameter("password", password);
Customer theCustomer = query.uniqueResult();
System.out.println("******************"+query.getResultList().size());
return theCustomer;
}
}
【问题讨论】:
-
到底是什么问题?
-
String hql="from Customer c where c.UserName=:username and c.password=:password";
-
我需要登录行数
-
不要请不要。您的代码存在多种缺陷,其中之一是使用纯密码。也不要尝试扮演你自己的机制,只需使用 Spring Security 并配置适当的查询。
标签: java spring spring-boot spring-data-jpa jpql