Examples of java SMS Verification Code obtaining limit and java SMS Verification Code

Source: Internet
Author: User

Examples of java SMS Verification Code obtaining limit and java SMS Verification Code

Currently, no matter what the project uses the text message verification function, the program will design a limit on the number of times the text message verification code is obtained, which is mainly to prevent the text message Verification Code interface from being refreshed.

In the previous section, I used the text message Verification Code login function for a project. I studied the following and posted it to share it.

The SMS interface involved here, with the third-party SMS interface-power thinking music letter (http://www.lx598.com/), if you want to know the SMS interface access, you can go to their official website, check the text message interface API documentation and refer to the following code to understand it.

The main code for user registration is as follows:

// Main js method: // obtain the mobile phone verification code: function getRegCode () {if ($. trim ($ ('# inputCaptcha '). val () = '') {signature ('invalid imgrs'verification .html (" the image Verification Code cannot be blank "); $ ('# inputCaptcha '). select (); return;} if (! IsPhoneNum ($ ('# phoneRe '). val () {document. getElementById ('phonereinfo '). innerHTML = '<font color = "red"> enter a valid 11-digit mobile phone number </font>';} else {document. getElementById ('phonereinfo '). innerHTML = 'Log On with your mobile phone number after registration '; $. ajax ({url: "$ {path}/account/checkMob", type: "POST", data: "account. ACCMOB = "+ $ ('# phoneRe '). val (), contentType: "application/x-www-form-urlencoded; charset = UTF-8", async: false, success: f Unction (data) {res = data; if (data = 1) {document. getElementById ('phonereinfo '). innerHTML = '<font color = "red"> the mobile phone number has been registered </font>'; refreshYzm ();} else {document. getElementById ('phonereinfo '). innerHTML = '<font color = "green"> this phone number is available </font>'; $. ajax ({url: "$ {path}/account/reAimcodeGetVeCode", type: "POST", data: "account. ACCMOB = "+ $ ('# phoneRe '). val () + "& fromSource = 4 & smsCount =" + $ ('# smsCount' ). Val () + "& searchName =" + $. trim ($ ('# inputCaptcha '). val (), contentType: "application/x-www-form-urlencoded; charset = UTF-8", async: false, success: function (data) {myArray = data. split ("&"); if (myArray [0] = 'sent successfully! ') {CanCaptcha = true; document. getElementById ('phonereinfo'). innerHTML =' <font color = "green"> the verification code has been sent. Check it carefully! </Font> accountFID = myArray [1];} else if (data = 'restrict requests') {document. getElementById ('phonereinfo '). innerHTML = '<font color = "red"> A mobile phone number can be applied up to three times a day! </Font> '; refreshYzm ();} else if (data = 'verification code error') {document. getElementById ('phonereinfo '). innerHTML = '<font color = "red"> Incorrect verification code! </Font> '; refreshYzm () ;}}, error: function () {alert (' exception, internal verification error! '+ Data) ;}}}}, error: function () {alert (' exception, verification error! ') ;}}) ;}Var smsCount = parseInt ($ (' # smsCount '). val (); smsCount = smsCount <3? SmsCount +; $ ('# smsCount'). val (smsCount );}
// Register a new user // account is the user class @ Action (value = "reAimcodeGetVeCode") public void reAimcodeGetVeCode () {PrintWriter out; String result = "Verification Code application failed! Please try again! "; Try {smsUnit = new SmsUnit (ConfUtil. getProperty (" sys_sms_server "); if (null! = Account. getACCMOB ()&&! Account. getACCMOB (). equals ("") {account. setACCSTATUS (new BigDecimal (1); // set the Usage Status: unused String verifyCode = String. valueOf (new Random (). nextInt (899999) + 100000); // generate the SMS Verification Code account. setFSECURITYCODE (verifyCode); account. setACCCREATEDATE (new Date (); Calendar c = Calendar. getInstance (); c. add (Calendar. DAY_OF_MONTH, 1); // set the verification code expiration time to 24-hour account. setFREGISTERSOURCE (fromSource); // set the registration source // determine whether the mobile phone has obtained the verification code CcountCriteria accountCriteria = new AccountCriteria (); accountCriteria. createCriteria (). andACCMOBEqualTo (account. getACCMOB (); List <Account> accs = accountService. selectByExample (accountCriteria); // verification code application count int re = 0; Integer cishu = 0; // if the user does not exist if (accs = null | accs. isEmpty () {cishu = 1; account. setSDKURL ("1"); account. setFSECURITYOUTTIME (c. getTime (); // set the verification code validity period BigDecimal accid = accoun TService. getPrimaryKey (); account. setFID (accid); re = accountService. insertSelective (account, IPUtil. getRealIP (request); Cookie cookie = new Cookie ("id", accid. toString (); cookie. setMaxAge (Integer. MAX_VALUE); response. addCookie (cookie);} else {Account ac = accs. get (0); account. setFID (ac. getFID (); Date date = new Date (); // determine the number of times the verification code is applied for by time. // if it is a new day, change the number of times to 1 if (date. getDate ()> = ac. getFSECURITYOU TTIME (). getDate () {account. setSDKURL ("1");} else {Integer count = Integer. parseInt (ac. getSDKURL (); account. setSDKURL (count + 1 + ""); // The number of times + 1} cishu = Integer if it is not a new day. parseInt (account. getSDKURL (); account. setFSECURITYOUTTIME (c. getTime (); if (cishu <= 3) re = accountService. updateByPrimaryKeySelective (account); // use the modification method to save the verification code sending information} if (re> 0 & cishu <= 3) {request. getSession (). removeAttribu Te (ConstValues. WEB_SESSION_PROMOTE); AccountCriteria ac = new AccountCriteria (); ac. createCriteria (). andACCMOBEqualTo (account. getACCMOB (); List <Account> acList = new ArrayList <Account> (); acList = accountService. selectByExample (ac); if (acList! = Null & acList. size ()> 0) {// here, execute the text message to send String content = "your verification code is:" + verifyCode + ", which is valid for 24 hours, this code can only be used once! [Text message signature] "; SendSmsReply sendSmsReply = smsUnit. sendSms (accName, accPwd, account. getACCMOB (), content, ""); // call a third-party API to send the SMS result = sendSmsReply. getReplyMsg () + "&" + acList. get (0 ). getFID () + "&" + acList. get (0 ). getSDKURL () ;}} else if (cishu> 3) {result = "restriction application" ;}} catch (Exception e) {logger. error ("failed to obtain verification code", e) ;}finally {try {response. setContentType ("text/html; charset = UTF-8"); response. setCharacterEncoding ("UTF-8"); out = response. getWriter (); out. write (result);} catch (IOException e) {logger. error ("", e );}}}
// This is the sending text message function of the third-party SMS interface of the Power thinking music mail. Refer to the code below: /*** send SMS ** @ param accName happy letter account username * @ param accPwd happy letter account password * @ param seed current time format: yyyymmdd hhmiss example: 20130806102030 * @ param aimcodes multiple mobile phone numbers are separated by commas (,). * @ param content is followed by a signature * @ param schTime. the scheduled time format is as follows: 08:00:00 * @ return the result returned by the server is OK: business id or error code */public static String sendSms (String accName, String accPwd, String mobies, String content, String schTime) {StringB Uffer sb = new StringBuffer ("http://sdk.lx198.com/sdk/send2? "); Try {String seed = new SimpleDateFormat (dateFormatStr ). format (new Date (); sb. append ("& accName =" + accName); sb. append ("& seed =" + seed); sb. append ("& accPwd =" + MD5.getMd5String (MD5.getMd5String (accPwd) + seed); sb. append ("& aimcodes =" + mobies); sb. append ("& schTime =" + URLEncoder. encode (schTime, "UTF-8"); // space punctuation for encode conversion sb. append ("& content =" + URLEncoder. encode (content, "UTF-8"); // encode conversion URL = new url (sb. toString (); HttpURLConnection connection = (HttpURLConnection) url. openConnection (); connection. setRequestMethod ("POST"); BufferedReader in = new BufferedReader (new InputStreamReader (url. openStream (); return in. readLine ();} catch (Exception e) {e. printStackTrace ();} return null ;}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.