【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>

 

posted @ 2020-02-03 23:20  一只桔子2233  阅读(1365)  评论(0编辑  收藏  举报