zoukankan      html  css  js  c++  java
  • 验证码的获取与验证

    一、验证码的获取(将相关内容存在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();
            }
        }
    }
  • 相关阅读:
    asp.net 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction
    mvc EF 执行SQL语句
    MVC 自定义AuthorizeAttribute实现权限管理
    EF
    sql 、linq、lambda 查询语句的区别
    说说C#中的enum吧
    C# A窗口内容显示在B窗口中的方法
    C# 程序开始主要是写类和方法 的基本步骤和调用方法
    C# 文件流基本操作步骤
    Ghost命令使用方法
  • 原文地址:https://www.cnblogs.com/sjshare/p/5477601.html
Copyright © 2011-2022 走看看