外部WebサイトでのSMSの検証は、ユーザー情報の登録、検証などなど、非常に必要です。SpringMVCの実装は次のとおりです。
SMSインターフェイス
SMSインターフェースでは、一部の企業はモバイルSMSプラットフォームインターフェイスを購入します。あなたが個人または中小企業の場合は、いくつかのクラウドサービスを使用できます。たとえば、BaiduのAPIストアで。
http://apistore.baidu.com/apiworks/serviceTail/1018.htmlを使用しています
もちろん、SMSインターフェイスを支払う必要があり、テンプレートに基づいています。特定の手順については、このWebサイトの指示を参照してください。
フロントエンドインターフェイス
次のとおりのフロントエンドインターフェイスは、[検証コードをクリックして[]をクリックしてから、ボタンが灰色になり、カウントダウンします。 (携帯電話番号は私のものです~~)
HTMLコードは書き込まれていません。JSは次のとおりです。検証コードは、検証コードを取得するボタンのIDです。電話は携帯電話番号のIDです。携帯電話番号は単なる確認です。より正確である場合は、規則性を使用してください。 URLのsendsmsは、バックグラウンドのspringmvcのパスです。
$( "#validationCode") {alert( "法的電話番号を入力してください");}}); var countdown = 60; function updatebuttontatus(){var phone = $( "#validationcode"); if(countdown == 0); if(countdown == 0){countdown.attr( "disabled"、 "fals"); phone.val( "free get visifilation code"); countdown = 60; {phone.attr( "disabled"、 "true"); phone.val( "resend(" + countdown + ")"); countdown - ;} setimeout(function(){updateButtonStatus()}、1000)}バックエンドコード
@RequestMapping(value = "/sendsms")@ResponseBodic String sendsms(@RequestParam( "Phone")String Phone、httpservletrequest request){stringbuilder code = new StringBuilder();ランダム= newランダム(); {code.append(string.valueof(random.nextint(10)));} httpsession session = request.getSession(); session.setattribute(validate_phone、phone); sesse.setattribute(validate_phone_code(validate_phone_code、code.tosttring()); session.setattribute(send_code_time(sed_code_time); is: "+code; smsutil.send(電話、smstext); return" success ";}Smsutilは、上記のカプセル化されたSMSインターフェイスの送信クラスです。以下を参照して、API_KEYを独自に変更してください。
パブリッククラス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){buffered reader; {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"); headerconnection.setRequestProperty( "apikey"、api_key); connect.connect(); inputstream is = connection.getInputStream(); reader = new bufferedReader(new inputstreamReader( "utf-8"); string strread = null; {sbf.append(strread); sbf.append( "/r/n");} reader.close(); result = sbf.tostring();} catch(exception e){e.printstacktrace();} return result;}}}フロントデスクにフォームを送信する前に、Ajaxを使用してフォームを確認して、検証コードが正しいかどうかを確認する必要があります。
@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";}上記は、編集者が紹介したスプリングMVCのSMS検証コード関数の実装方法です。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!