码迷,mamicode.com
首页 > 其他好文 > 详细

【SSM 验证码】登录验证码

时间:2020-02-04 00:43:58      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:apt   bsp   cli   public   color   put   Servle   click   hashmap   

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>

 

【SSM 验证码】登录验证码

标签:apt   bsp   cli   public   color   put   Servle   click   hashmap   

原文地址:https://www.cnblogs.com/kikyoqiang/p/12258007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!