ems-jsp 用户登录模块

整体思路

1.登录首先要从前端获取账号和密码

2.根据用户名去查询用户,用户存在 比对密码(注意 由于密码是加密存入数据库的 所以比对时要用用户输入的密码加密后跟数据库中的密码比对)。

   用户不存在,登陆失败,返回登录界面并且显示失败原因。

代码:展示部分

Controller层:

@RequestMapping("login")
    public String login(String username,String password,HttpSession session) throws UnsupportedEncodingException {
        log.debug("接收到的用户名:{},接受到的密码是:{}",username,password);
        //1.根据用户名去查询用户
        try {
            User user = userService.login(username,password);
            //登录成功
            session.setAttribute("user",user);
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/login.jsp?msg="+URLEncoder.encode(e.getMessage(),"UTF-8");
        }

        return "redirect:/employee/list";
    }

sevice层实现:

    @Override
    public User login(String username, String password) {
        //1.根据用户输入的用户名查询数据中是否存在
        User user = userDao.findByUserName(username);
        //2.判断对象是否存在
        if(ObjectUtils.isEmpty(user)) throw new RuntimeException("用户名输入错误!");
        //3.判断密码的正确性
        String digestPassword = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        if(!user.getPassword().equals(digestPassword)) throw new RuntimeException("密码输入错误!");
        return user;
    }

页面:

 

 

posted @ 2024-03-04 21:16  神行乌龟  阅读(34)  评论(0)    收藏  举报