解释如下代码: public String checkLogin(String account, String password, HttpSession hs, ModelMap mm){ Admin admin=ad.checkLogin(account,password); if (admin!=null){ hs.setAttribute("admin",admin); return "redirect:findAll.do"; }else { mm.put("error","账号或者密码错误"); return "login"; } }
时间: 2024-02-14 11:09:37 浏览: 27
这段代码是一个登录验证的方法,其中传入了四个参数:账号、密码、HttpSession对象和ModelMap对象。该方法的作用是检查传入的账号和密码是否与数据库中的信息匹配,如果匹配成功则将用户信息存入 HttpSession 对象中,并且重定向到指定的页面;如果匹配失败,则将错误信息存入 ModelMap 对象中,并返回登录页面。具体实现过程可能需要根据上下文来理解。
相关问题
@RequestMapping("/login") public String login(@RequestBody User user, @RequestParam String checkCode, HttpServletRequest request) throws IOException { HttpSession session = request.getSession(); String checkCodeGen = (String) session.getAttribute("checkCodeGen"); if (checkCodeGen.equalsIgnoreCase(checkCode)){ User result = userService.login(user.getUserName(), user.getPassword()); if (result != null){ session.setAttribute("userName",result.getUserName()); return "success" ; } }else{ return "验证码错误"; } return "用户名或密码错误"; } @RequestMapping("/checkCode") public void checkCode(HttpServletRequest request, HttpServletResponse response) throws IOException { String checkCodeGen = CheckCodeUtil.generateVerifyCode(4); HttpSession session = request.getSession(); session.setAttribute("checkCodeGen",checkCodeGen); System.out.println(session.getId()); ServletOutputStream os = response.getOutputStream(); CheckCodeUtil.outputImage(100,40,os,checkCodeGen); } @RequestMapping("/checkLogin") public String checkLogin(HttpSession session){ return (String)session.getAttribute("userName"); } @RequestMapping("/logout") public String logout(HttpSession session){ session.setAttribute("userName",null); return "success"; }
这段代码是一个基于Spring MVC框架的用户登录、验证码验证、注销功能的实现。其中:
- @RequestMapping是一个注解,用于映射请求的URL路径和Controller方法。
- @RequestBody注解用于将request请求的body部分转换成Java对象,并且可以通过@RequestParam注解获取请求参数。
- HttpSession是一个接口,用于存储和获取当前会话的数据。
- CheckCodeUtil是一个工具类,用于生成和输出验证码图片。
具体实现逻辑如下:
1. 用户访问/login路径,请求会被映射到login方法。
2. login方法首先从request请求中获取用户提交的用户名和密码,以及验证码。
3. 然后从session中获取之前生成的验证码,并与用户提交的验证码进行比较,判断是否输入正确。
4. 如果验证码输入正确,则调用userService的login方法进行用户登录验证,如果成功则将用户名存入session,并返回"success"字符串表示登录成功。
5. 如果验证码输入错误,则直接返回"验证码错误"字符串。
6. 如果用户名或密码错误,则返回"用户名或密码错误"字符串。
7. 用户可以在页面上访问/checkCode路径来获取验证码图片,该请求会被映射到checkCode方法。该方法通过CheckCodeUtil工具类生成验证码图片,并将验证码存入session中。
8. 用户可以在页面上访问/checkLogin路径来检查当前用户是否已经登录,该请求会被映射到checkLogin方法。该方法从session中获取当前用户的用户名,并返回该值。
9. 用户可以在页面上访问/logout路径来退出登录,该请求会被映射到logout方法。该方法将session中的用户名设为null,并返回"success"字符串表示注销成功。
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class); if (annotation != null) { if (this.isRepeatSubmit(request, annotation)) { AjaxResult ajaxResult = AjaxResult.error(annotation.message()); ServletUtils.renderString(response, JSON.toJSONString(ajaxResult)); return false; } } } boolean flag = false; TokenService tokenService = SpringUtils.getBean(TokenService.class); LoginAppUser loginUser = tokenService.getLoginUser(request); if (loginUser != null && loginUser.getGuest() != null && loginUser.getGuest().getGuestNum() != null) { tokenService.verifyToken(loginUser); String guestNum = loginUser.getGuest().getGuestNum(); HttpSession httpSession = request.getSession(); RedisCache redisUtil = SpringUtils.getBean(RedisCache.class); Guest guest = redisUtil.getCacheMapValue(RedisConstants.GUEST_MAP, guestNum); SysGuest sysGuest = JSON.parseObject(JSON.toJSONString(guest), SysGuest.class); if (sysGuest == null) { flag = false; httpSession.removeAttribute(Constants.SERVER_TYPE_APP); } else { if (httpSession.getAttribute(Constants.SERVER_TYPE_APP) == null) { httpSession.setAttribute(Constants.SERVER_TYPE_APP , sysGuest); } return true; } } 优化这段代码
首先,可以将该方法拆分成两个方法,一个处理重复提交的逻辑,一个处理登录用户的逻辑,以提高代码可读性和可维护性。
处理重复提交的逻辑可以改为使用注解方式,遍历所有的参数,判断是否存在 @Token 注解,如果存在则验证 token 是否一致。
处理登录用户的逻辑可以简化代码,将获取当前登录用户和验证 token 的逻辑移动到 TokenService 中,简化该方法代码。
重构后的代码如下所示:
```
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
Token annotation = method.getAnnotation(Token.class);
if (annotation != null) {
if (!tokenService.verifyToken(request)) {
AjaxResult ajaxResult = AjaxResult.error(annotation.message());
ServletUtils.renderString(response, JSON.toJSONString(ajaxResult));
return false;
}
}
}
if (tokenService.checkLogin(request)) {
HttpSession session = request.getSession();
if (session.getAttribute(Constants.SERVER_TYPE_APP) == null) {
session.setAttribute(Constants.SERVER_TYPE_APP, tokenService.getLoginUser(request).getGuest());
}
return true;
} else {
ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(HttpStatus.UNAUTHORIZED.value(), "未登录或登录超时。")));
return false;
}
}
```
其中,`TokenService` 的实现中包括了 `verifyToken` 和 `checkLogin` 方法,分别用于验证 token 和检查当前用户是否已经登录。同时,重复提交的逻辑使用 `@Token` 注解进行处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)