验证码的获取与验证

一、验证码的获取(将相关内容存在session中);

public class SendValidMsg extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 如果不存在 session 会话,则创建一个 session 对象
        HttpSession session = req.getSession(true);

        String phoneNum = req.getParameter("phone");
        String validCode = "";

        int times = 0;
        String msg = "";
        boolean sendflag = true;
        validCode = StringUtil.getStr(session.getAttribute("validcode" + phoneNum));
        times = NumberUtil.getIntegerValue(session.getAttribute("times" + phoneNum));
        if (times == 3) {
            msg = "您已超过最多重发次数限制!";
            sendflag = false;
        } else if (times > 3) {
            msg = "验证码已失效!";
            sendflag = false;
        } else {
            msg = "验证码发送成功!";
        }
        times += 1;
        if (validCode.equals("")) {
            validCode = StringUtil.getRandNum(4);//获取4位随机数
        }
        session.setAttribute("validcode" + phoneNum, validCode);
        session.setAttribute("times" + phoneNum, times);
        session.setAttribute("sendtime" + phoneNum, DateUtil.date2Str(new Date(), "yyyy-MM-dd HH:mm:ss"));

        if (sendflag) {
            MessageClient.sendMessage(phoneNum, "尊敬的用户您的验证码:" + validCode + ",请在5分钟内使用。回复TD退订。");
        }

        JSONObject resultObj = new JSONObject();
        resultObj.put("msg", msg);
        resultObj.put("success", true);
        try {
            resp.setContentType("application/x-json");
            resp.setCharacterEncoding("UTF-8");
            PrintWriter out = resp.getWriter();
            out.write(resultObj.toJSONString());
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

二、验证。

public class ValidMsg extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 如果不存在 session 会话,则创建一个 session 对象
        HttpSession session = req.getSession(true);

        String validCode = "";
        String phoneNum = req.getParameter("phone");
        int times = 0;
        String msg = "";
        String validCode1 = "";
        validCode = StringUtil.getStr(session.getAttribute("validcode" + phoneNum)); //存在session中的验证码
        times = NumberUtil.getIntegerValue(session.getAttribute("times" + phoneNum)); //发送次数,发送3次以上验证码失效。
        int validTimes = 0;
        Long intervalTime = (long) 0;
        validTimes = NumberUtil.getIntegerValue(session.getAttribute("validtimes" + phoneNum)); //验证次数,错误验证5次以上验证码失效。
        Date now = new Date();   //当前时间
        Date sendTime = DateUtil.getDate(StringUtil.getStr(session.getAttribute("sendtime" + phoneNum)), "yyyy-MM-dd HH:mm:ss"); //从session中取验证码发送时间
        if (sendTime != null) {
            intervalTime = now.getTime() - sendTime.getTime();
        }
        boolean validflag = false;   //是否验证通过
        if (times > 3 || intervalTime > 5 * 60 * 1000 || validTimes > 5) {
            msg = "验证码已失效,请重新打开浏览器再次注册!";
        } else {
            validCode1 = req.getParameter("validcode");   //输入的验证码
            if (validCode1.toLowerCase().equals(validCode.toLowerCase()) && !validCode.toLowerCase().equals("")) {
                //msg = "验证通过!";
                validflag = true;
            } else {
                msg = "验证码输入错误!";
                validTimes += 1;
                session.setAttribute("validtimes" + phoneNum, validTimes);
            }
        }

        JSONObject resultObj = new JSONObject();
        resultObj.put("msg", msg);
        resultObj.put("validflag", validflag);
        resultObj.put("success", true);
        try {
            resp.setContentType("application/x-json");
            resp.setCharacterEncoding("UTF-8");
            PrintWriter out = resp.getWriter();
            out.write(resultObj.toJSONString());
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
posted @ 2016-05-10 13:58  技术宅home  阅读(3906)  评论(0编辑  收藏  举报