1.分析
登录与退出功能是整个项目中最简单的功能,实现这两个功能,基本只要思路理好了,代码就随便写了,根据我的项目总结,登录功能的思路基本如下:
①. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
②. 根据页面提交的用户名username查询数据库中员工数据信息
③. 如果没有查询到, 则返回登录失败结果
④. 密码比对,如果不一致, 则返回登录失败结果
⑥. 登录成功,将员工id存入Session, 并返回登录成功结果
2.代码实现
@PostMapping("/login")
public R login(HttpServletRequest request, @RequestBody Employee employee){
//①. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
//②. 根据页面提交的用户名username查询数据库中员工数据信息
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Employee::getUsername, employee.getUsername());
Employee em = employeeService.getOne(queryWrapper);
//③. 如果没有查询到, 则返回登录失败结果
if(em == null){
return R.error("登录失败");
}
//④. 密码比对,如果不一致, 则返回登录失败结果
if(!password.equals(em.getPassword())){
return R.error("登录失败");
}
//⑤. 查看员工状态,如果为已禁用状态,则返回员工已禁用结果
if(em.getStatus() == 0){
return R.error("该用户已被禁用");
}
//⑥. 登录成功,将员工id存入Session, 并返回登录成功结果
request.getSession().setAttribute("employee",em.getId());
return R.success(em);
}
@PostMapping("/logout")
public R logout(HttpServletRequest request){
request.getSession().removeAttribute("employee");
return R.success("退出成功");
}
3.总结
这里是简单的登录功能实现,思路理清是实现功能的前提!