• 【SSM 验证码】登录验证码


    LoginController
        /**
         * 登陆方法
         */
        @ResponseBody
        @RequestMapping("login2")
        public Map<String, String> login2(UserVo userVo, Model model) {
            Map<String, String> res = new HashMap();
            String code = WebUtils.getHttpSession().getAttribute("code").toString();
            if(!userVo.getCode().equals(code)){
                model.addAttribute("error", SysConstast.USER_LOGIN_ERROR_MSG);
                res.put("code", "400");
                res.put("message", "验证码错误");
                return res;
            }
            User user = this.userService.login(userVo);
            if (null == user) {
                model.addAttribute("error", SysConstast.USER_LOGIN_ERROR_MSG);
                res.put("code", "302");
                res.put("address", "../login/toLogin.action");
                return res;
            }
            //放到session
            WebUtils.getHttpSession().setAttribute("user", user);
            //记录登陆日志 向sys_login_log里面插入数据
            LogInfoVo logInfoVo = new LogInfoVo();
            logInfoVo.setLogintime(new Date());
            logInfoVo.setLoginname(user.getRealname() + "-" + user.getLoginname());
            logInfoVo.setLoginip(WebUtils.getHttpServletRequest().getRemoteAddr());
    
            logInfoService.addLogInfo(logInfoVo);
            res.put("code", "200");
            res.put("address", "../login/mainIndex.action");
            return res;
        }
    
        @RequestMapping("mainIndex")
        public String mainIndex() {
            return "system/main/index";
        }
    
        /**
         * 得到登陆验证码
         *
         * @throws IOException
         */
        @RequestMapping("getCode")
        public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
            // 定义图形验证码的长和宽
            LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 5);
            session.setAttribute("code", lineCaptcha.getCode());
            ServletOutputStream outputStream = response.getOutputStream();
            ImageIO.write(lineCaptcha.getImage(), "JPEG", outputStream);
        }

     login.jsp

        <div class="layui-form-item input-item" id="imgCode">
            <label for="code">验证码</label>
            <input type="text" placeholder="请输入验证码" autocomplete="off" name="code" id="code" class="layui-input">
            <img src="${ctx}/login/getCode.action" onclick="this.src=this.src+'?'">
        </div>
  • 相关阅读:
    python3安装 feedparser
    numpy
    Git详细教程(1)---个人Git的基本使用
    JavaScript高级程序设计---学习笔记(五)
    JavaScript高级程序设计---学习笔记(四)
    JavaScript高级程序设计---学习笔记(三)
    JavaScript高级程序设计---学习笔记(二)
    JavaScript高级程序设计---学习笔记(一)
    JavaScript 基础阶段测试题
    JS实现购物车特效
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/12258007.html
Copyright © 2020-2023  润新知