Verification of SMS in external websites is very necessary, such as when registering, verifying user information, etc. The implementation in SpringMVC is as follows:
SMS interface
SMS interface, some companies will purchase mobile SMS platform interfaces. If you are an individual or a small business, you can use some cloud services. For example, on Baidu's API Store.
I'm using: http://apistore.baidu.com/apiworks/servicedetail/1018.html
Of course, the SMS interface must be paid, and it is based on the template. For the specific instructions, please refer to the instructions in this website.
Front-end interface
The front-end interface, which may be as follows, click to get the verification code, and then the button turns gray and counts down. (The mobile phone number is mine ~~)
The HTML code is not written, JS is as follows: verificationCode is the ID of the button to obtain the verification code. The phone is the ID of the mobile phone number. The mobile phone number is just a simple verification. If it is to be more accurate, use regularity. The sendSms of the url is the path of springMVC in the background.
$("#validationCode").click(function(){var phone = $("#phone").val();if($("#phone").val() && $("#phone").val().length == 11){$.ajax({cache : false,url : "sendSms",data : {phone : phone}});updateButtonStatus();}else {alert("Please enter a legal phone number");}});var countdown=60;function updateButtonStatus(){var phone = $("#validationCode");if (countdown == 0) {phone.attr("disabled","false");phone.val("Free to get verification code");countdown = 60;return;} else {phone.attr("disabled","true");phone.val("Resend(" + countdown + ")");countdown--;}setTimeout(function() {updateButtonStatus() },1000)}Backend code
@RequestMapping(value = "/sendSms")@ResponseBodypublic String sendSMS(@RequestParam("phone") String phone, HttpServletRequest request){StringBuilder code = new StringBuilder();Random random = new Random();// Generate 6-bit verification code for (int i = 0; i < 6; i++) {code.append(String.valueOf(random.nextInt(10)));}HttpSession session = request.getSession();session.setAttribute(VALIDATE_PHONE, phone);session.setAttribute(VALIDATE_PHONE_CODE, code.toString());session.setAttribute(SEND_CODE_TIME, new Date().getTime());String smsText = "Your verification code is:"+code;SMSUtil.send(phone,smsText);return "success";}The SMSUtil is the sending class of the SMS interface encapsulated above. Refer to the following, change the API_KEY to your own.
public class SMSUtil {static String httpUrl = "http://apis.baidu.com/kingtto_media/106sms/106sms";final static String API_KEY = "xxxx";public static String send(String phone,String content) {BufferedReader reader = null;String result = null;StringBuffer sbf = new StringBuffer();try {String httpArg = "mobile="+phone+"&content="+URLEncoder.encode(content,"UTF-8")+"&tag=2";httpUrl = httpUrl + "?" + httpArg ;URL url = new URL(httpUrl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");// Fill in the apikey to HTTP headerconnection.setRequestProperty("apikey",API_KEY);connect.connect();InputStream is = connection.getInputStream();reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));String strRead = null;while ((strRead = reader.readLine()) != null) {sbf.append(strRead);sbf.append("/r/n");}reader.close();result = sbf.toString();} catch (Exception e) {e.printStackTrace();}return result;}}Before submitting the form in the front desk, you also need to use ajax to verify the form to verify whether the verification code is correct:
@RequestMapping("/validate")@ResponseBodyprotected String validate(HttpServletRequest request,@RequestParam("phone") String inputPhone,@RequestParam ("code") String inputCode){HttpSession session = request.getSession();String code = (String) session.getAttribute(VALIDATE_PHONE_CODE);String phone = (String) session.getAttribute(VALIDATE_PHONE);if(phone.equals(inputPhone) && code.equalsIgnoreCase(inputCode)){return "success";}else{return "failure";}}The above is the implementation method of Spring MVC's SMS verification code function introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!