Java backend implements SMS verification code

Overall process:

  1. The customer fills in the mobile phone number, and clicks the Get Verification Code button through the client to verify whether the mobile phone number is valid. If it is valid, the client sends a request to the background server, and the client starts a countdown of 60s. If it fails, it will return;

  2. The server verifies whether the mobile phone number is registered or valid. If passed, call the third-party SMS communication interface and send relevant data (including mobile phone number and verification code), and then call back the result. If successful, the verification code will be stored in the session, and if it fails, a prompt will be returned. , return if it fails.

  3. After the customer receives the verification code, within the valid time, fill in and send the request.

  4. On the server side, after receiving the request, the verification code sent by the user is compared with the verification code put into the session in advance, and the same is passed, otherwise the verification code is invalid.

After passing, the verification code in the session needs to be invalidated, which is generally set to empty.

The first pseudo code:


function sendCaptcha(tel) {    

       console.log("sendCaptcha: tel = " + tel);    

       $.ajax({    

           type: 'post',    

           url: '/sms/captcha/' + tel,    

           dataType: "json",    

           success: function (data) {    

               console.log("sendCaptcha ==> success: data = " + eval(data));    

               if (data) {    

                   countdown();    

                   b_code = false;    

               } else {    

                   alert("您发送的频率过快!");    

               }    

           },    

           error: function (data) {    

               console.log("sendCaptcha ==> error: data = " + eval(data));    

               alert("网络超时");    

               clearTimeout(t);    

               b_code = true;    

               var msg = "获取验证码";    

               $("#code").text(msg);    

               c = 60;    

           }    

       });    

   }    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

The second step pseudo code:


@RequestMapping(value = "captcha/{recPhoneNum}", method = RequestMethod.POST)    

   public Object getSmsCaptcha(ModelMap model, @PathVariable("recPhoneNum")String recPhoneNum) {    

       String responseBody = null;    



       /* 这里验证手机号是否被注册 */



      // 生成验证码

       String captcha  = Generator.generateCaptcha();    



      // 第三方短信通信接口参数设置

     req.setReceive(recPhoneNum);

       try {    

          // 发送请求

           responseBody = req.send();

          // 将验证码放入session

           model.addAttribute("captcha", captcha);    

          // 得到结果

           responseBody = rsp.getBody();    

           log.debug("getSmsCaptcha: responseBody = " + responseBody);    

           if (rsp.getResult() != null) {    

               model.addAttribute("success_response", rsp.getResult());    

           } else {    

               model.addAttribute("error_response", rsp.getSubMsg());    

           }    

       } catch (ApiException e) {    

           log.error("getSmsCaptcha :" + e.getErrMsg());    

       }    

        // 解析结果

       if (successJson != null) {    

           successJson = successJson.getJSONObject("result");    

           return successJson.getBoolean("success");    

       } else {    

           return false;    

       }    

   }    




  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

Pseudocode for the last step:


// 从session取出验证码

String captcha = session.getAttribute("captcha");

// 比较

if (reqCaptcha.equals(captcha))

// 相同通过,则无效化验证码

session.setAttribute("captcha", null);


else

// 不通过并提示无效验证码

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324696685&siteId=291194637