zoukankan      html  css  js  c++  java
  • 短信发送与验证

    这次任务是写一个短信验证功能。

    1 发送接口

     1 @RequestMapping("sendSMSVerification")
     2     @ResponseBody
     3     public Result sendSMSVerification(HttpServletRequest request,String type){
     4         PcUser user = getUser(request);
     5         String mobile = user.getUserAccount();
     6         Result result =new Result();
     7         boolean phone = PhoneUtils.isPhone(mobile);
     8         if (mobile==null) {
     9             result.setResultCode(new ResultCode(-1, "手机号为空"));
    10             return result;
    11         }
    12         if (!phone) {
    13             result.setResultCode(new ResultCode(-1, "手机号格式不正确"));
    14             return result;
    15         }
    16         try {
    17             ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
    18             String redisKey = "SMS_XCX_LOCK:Phone=" + mobile+"Type="+type;
    19             String val = valueOps.get(redisKey);
    20             if (com.jiutong.lang.StringUtils.isNotBlank(val)) {
    21                 result.setResultCode(new ResultCode(-1, "请勿重复发验证码"));
    22                 log.warn("warn sendIdentifyCode res={}", JSON.toJSONString(result));
    23                 return result;
    24             }
    25             //验证码
    26             String Code = com.jiutong.lang.CommonUtils.getRandomNumber(1000, 9999) + "";
    27             //测试定死
    28 //            int code = smsService.sendSMSByZml(mobile,Code,SmsScene.WELCOME_TO_USE_AUTO_PARTS);
    29             int code =0;
    30             if (0 == code) {
    31                 //验证码5分钟过期
    32 //                valueOps.set(redisKey, Code, 5, TimeUnit.MINUTES);
    33                 result.setResultCode(new ResultCode(0, "短信发送成功"));
    34             } else {
    35                 result.setResultCode(new ResultCode(-1, "短信发送失败"));
    36             }
    37             log.info("end sendIdentifyCode res={}", JSON.toJSONString(result));
    38         }catch (Exception e) {
    39             e.printStackTrace();
    40         }
    41         return  result;
    42     }

    这里的用户信息是通过session里获取的。

    把经常用的获取用户信息,写成一个方法。

    public void setUser(HttpServletRequest request, PcUser user, HttpServletResponse response) {
            HttpSession session = request.getSession();
            session.setMaxInactiveInterval(3 * 60 * 60);
            session.setAttribute("user", user);
        }

    2 其中对手机号做了处理,判断是否符合手机规则,也有方法。

    3 这里做了判断,因为短信有失效时间,所以放进了缓存里,所以只这之前要判断一下缓存是否有之前的数据。

     4 验证码就是4位或6位 这里自己实际需求选择。

    String Code = com.jiutong.lang.CommonUtils.getRandomNumber(1000, 9999) + "";

    5 接下来就是关键方法,发送验证码。这里对接的是阿里云短信服务。

     5.1

     这里对短信的场景有枚举。

    6 重点是这里

     这里是根据阿里的短信文档,自己合理设计。

    7 最后是验证短信。

     根据key,查redis缓存。也是比较简单的。

  • 相关阅读:
    C#练习3
    C#练习2
    C#环境变量配置及csc命令详解(转自cy88310)
    建站流程(转)
    C#练习
    程序竞赛1
    排序算法
    输出有向图的邻接矩阵
    C#高效分页代码(不用存储过程)
    存储过程详解
  • 原文地址:https://www.cnblogs.com/zq1003/p/14362454.html
Copyright © 2011-2022 走看看