分为前端和后端
后端
大概任务是:
- 配置 doLogin 接口,实现传入 username, password, captcha 来进行验证,并返回 Response 封装的json对象, message 中存对应的消息
- 在验证过程中,需要通过httpSession来获取正确的captcha来验证,如果验证码错误,那么就直接返回一个login视图并添加Model参数isCaptchaFailure为true来提示用户
- 如果验证码通过而用户账户或密码错误,那么就设置 Response 的消息为 账户名或密码错误, success 字段为 false
- 若都成功则 设置 Response 消息为登录成功 success 字段为 true
controller
@Resource
UserService userService;
@PostMapping(value = "/login", produces = "application/json;charset=UTF-8")
@ResponseBody
public Response login(String username,String password, String captcha, HttpSession httpSession, Model model) {
log.info("username: " + username + ", password: " + password + ", captcha: " + captcha);
String sessionCaptcha = (String) httpSession.getAttribute("captcha");
log.info("sessionCaptcha: " + sessionCaptcha);
if (captcha == null || !captcha.equals(sessionCaptcha)) {
model.addAttribute("isCaptchaFailure", true);
return Response.failure("验证码错误");
} return userService.authenUser(username, password);
}
service
@Resource
UserRepository userRepository;
@Override
public Response authenUser(String username, String password) {
User user = userRepository.findByUsername(username);
if(user == null) {
return Response.failure("用户名不存在");
}else if(!user.getPassword().equals(password)) {
return Response.failure("密码错误");
} return Response.success("登录成功");
}