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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM