• 验证码的获取与验证


    一、验证码的获取(将相关内容存在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();
            }
        }
    }
  • 相关阅读:
    就是能打开网站的首页,但不能打开二级链接的解决办法
    .NET学习笔记:文件和注册表操作
    小猪今天生病了,要打针了
    Android基础教程——在TextView中显示Html 自定义标签,获取标签属性
    所谓周末,就是在家好好做饭、好好睡觉、好好玩游戏
    我开通了个人博客
    《和空姐一起的日子》中顾太太新买的电脑
    iPhone & iPad apps with special url shortcuts
    iPhone开发错误集
    瞧瞧包拯,狄仁杰,宋慈的团队
  • 原文地址:https://www.cnblogs.com/sjshare/p/5477601.html
Copyright © 2020-2023  润新知