前后端分離生成手機驗證碼


1.問題怎么判斷是否是同一個手機提交的驗證碼(保存到redis中通過手機號加一個參數就可以完成保存)

2.redis怎么存儲並且判斷在一段時間內不能夠再發消息(key,contant)contan可以是"time + contant"的字符串,也可以是保存兩個字段的對象

  1. 前台發起驗證碼發送申請(Ajax),並且按鈕倒計時、禁用
  2. 后台接收到請求,判斷圖片驗證碼是否合法
  3. 判斷上一次是否有一個未使用的有效驗證碼
  4. 如果有,並且過了重發時間(兩次發送時間大於60S)使用上一次有效驗證碼
  5. 如果沒有,創建一個新的短信驗證碼
  6. 把短信驗證碼存儲到Redis,格式如: SMS:18244229575={code:”1234” , sendTime: “mills”}
  7. 調用第三方短信網關發送短信驗證碼
  8. DB存儲驗證碼發送記錄作為結算依據

傳入的參數是手機號,圖片驗證碼,及保存圖片驗證碼的seesion值,這樣才可以通過該值和找到redis中的值判斷工具類在圖片驗證碼有工具類

 

@Override
    public void sendSmsCode(VerifyCodeAndSendPhoneDto dto) {
        //1.前台發起驗證碼發送申請(Ajax),並且按鈕倒計時、禁用
        String imageCode = dto.getImageCode();
        String imageCodeKey = dto.getImageCodeKey();
        String mobile = dto.getMobile();
        //2.后台接收到請求,判斷圖片驗證碼是否合法
        //這里一定需要后台驗證,因為前台的請求可以跳過
        BasicVerify(imageCode, imageCodeKey, mobile);
        //判斷圖片驗證碼時候正確
        AjaxResult ajaxResult = redisFegin.get(imageCodeKey);
        if (!ajaxResult.isSuccess() && ajaxResult.getResultObj()==null){
            throw new RuntimeException("圖片驗證已過期請重試");
        }
        String string = ajaxResult.getResultObj().toString();
        if(!imageCode.toLowerCase().equals(string.toLowerCase())){
            throw new RuntimeException("圖片驗證錯誤請重試");
        }
        String sms = null;
        //獲得一個有效的Key存到redis
        long now = new Date().getTime();
        String phoneKey= RegstContant.PHONE_REGIST_PRE+mobile;
        //3.判斷上一次是否有一個未使用的有效驗證碼
        AjaxResult lastReuslt = redisFegin.get(phoneKey);
        PhoneSendVo phoneSendVo = null;
        if(lastReuslt.getResultObj()!=null && lastReuslt.isSuccess()){
            //4.如果有,並且過了重發時間(兩次發送時間大於60S)使用上一次有效驗證碼
            phoneSendVo = JSON.parseObject(lastReuslt.getResultObj().toString(), PhoneSendVo.class);
            if((phoneSendVo.getTime()-now)/1000>60){
                throw new RuntimeException("請不要在一分鍾內再發短信");
            }
            sms = phoneSendVo.getSMS();
        }else {
            sms = StrUtils.getRandomString(6);
        }
        //5.如果沒有,創建一個新的短信驗證碼保存到redis
        phoneSendVo =new PhoneSendVo(sms, now);
        //6.把短信驗證碼存儲到Redis,格式如: SMS:18244229575={code:”1234” , sendTime: “mills”}
        //問題怎么存儲時間
        AjaxResult setexResult = redisFegin.setex(phoneKey, 600, JSON.toJSONString(phoneSendVo));
        if (!setexResult.isSuccess()){
            throw new RuntimeException("手機驗證碼Redis保存錯誤");
        }//發短信
        System.out.println("你的短信為【"+sms+"】,請在10分鍾之內校驗");
        System.out.println("保存到數據庫");
    }

    private void BasicVerify(String imageCode, String imageCodeKey, String mobile) {
        if(StringUtils.isEmpty(imageCode)){
            throw new RuntimeException("請輸入圖片驗證碼");
        }
        if(StringUtils.isEmpty(imageCodeKey)){
            throw new RuntimeException("請刷新頁面,重新輸入圖片驗證碼");
        }
        if(StringUtils.isEmpty(mobile)){
            throw new RuntimeException("請輸入手機號");
        }
    }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM