The basic idea of java to realize SMS verification

  • 2020-11-20 06:07:53
  • OfStack

This article shares the specific code of java to realize SMS verification for your reference. The specific content is as follows

Overall process:

Immediately, through introducing the customer to fill the client click access authentication code button, verify the phone number is valid, effective  client sends a request to the backend server and the client start the countdown 60 s, not through the return; The server verifies whether the mobile phone number is registered or valid. If it passes, it calls the SMS communication interface of the third party and sends relevant data (including the mobile phone number and verification code), and then calls back the result. If it succeeds, the verification code will be saved into session; if it fails, prompt will be returned; if it fails, it will be returned. After receiving the verification code, the customer fills in and sends the request within the valid time. On the server side, after receiving the request, the verification code sent by the user will be compared with the verification code put into session in advance, and the same verification code will pass, otherwise the verification code will be invalid.

After passing, the captcha in session needs to be invalidated, and 1 is generally set to null.

Step 1 Pseudocode:


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(" The frequency you sent is too fast !");  

        }  

      },  

      error: function (data) {  

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

        alert(" Network timeout ");  

        clearTimeout(t);  

        b_code = true;  

        var msg = " Get verification code ";  

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

        c = 60;  

      }  

    });  

  }  

Step 2 Pseudocode:


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

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

    String responseBody = null;  


    /*  Verify that the phone number is registered here  */


   //  Generate verification code 

    String captcha = Generator.generateCaptcha();  


   //  The first 3 Setting of interface parameters for SMS communication 

   req.setReceive(recPhoneNum);

    try {  

     //  Send the request 

      responseBody = req.send();

     //  Put the verification code in session

      model.addAttribute("captcha", captcha);  

     //  results 

      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());  

    }  

    //  Analytical results 

    if (successJson != null) {  

      successJson = successJson.getJSONObject("result");  

      return successJson.getBoolean("success");  

    } else {  

      return false;  

    }  

  }  

The last 1 step pseudocode:


//  from session Fetch verification code 
String captcha = session.getAttribute("captcha");
//  To compare 
if (reqCaptcha.equals(captcha))
//  The same pass, then invalid verification code 
session.setAttribute("captcha", null);

else
//  Does not pass and prompts for invalid verification code 


If in doubt, please point out!


Related articles: