This example shares the specific code for Java to implement mobile SMS verification for your reference. The specific content is as follows
Overall process:
After passing, the verification code in the session needs to be invalidated, which is generally set to empty.
The first step is 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("You send 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 whether the mobile phone number is registered here*/ // Generate verification code String captcha = Generator.generateCaptcha(); // Set the third-party SMS communication interface parameter to req.setReceive(recPhoneNum); try { // Send a request responseBody = req.send(); // Put the verification code in session model.addAttribute("captcha", captcha); // Get the result 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()); } // parse result if (successJson != null) { successJson = successJson.getJSONObject("result"); return successJson.getBoolean("success"); } else { return false; } }The last step pseudocode:
// Take out the verification code from the session String captcha = session.getAttribute("captcha");// Compare if (reqCaptcha.equals(captcha))// If the same passes, the verification code session.setAttribute("captcha", null);else// If it fails and prompts the invalid verification codeIf you have any questions, please point it out!
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.