码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC中web短信验证码校验<非原创>

时间:2015-05-15 19:24:06      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title></title>  
  6. </head>  
  7. <body onload="checkTime();">  
  8.     <div align="center">  
  9.         <div>  
  10.             <form id="validateForm" action="validate.html">  
  11.                 <div>手机号:<input id="phone" type="text" name="phone" value="" maxlength="11"/></div>  
  12.                 <div>验证码:<input type="text" name="code"><button id="validationCode" type="button" onclick="sendCode(this);" title="获取验证码">获取验证码</button></div>  
  13.                 <div><button type="button" onclick="doValidation();" title="提交">提交</button></div>  
  14.             </form>  
  15.         </div>  
  16.     </div>  
  17. </body>  
  18. </html>  

 

[javascript] view plaincopy技术分享技术分享
  1. <script type="text/javascript">  
  2.     function sendCode(obj){  
  3.         var phone = document.getElementById("phone");  
  4.         var value = phone.value.trim();  
  5.         if(value && value.length == 11){  
  6.             $.ajax({  
  7.                 cache : false,  
  8.                 url : "sendCode.html",  
  9.                 data : {phone : value}  
  10.             });  
  11.             // 1分钟内禁止点击  
  12.             for (var i = 1; i <= 60; i++) {  
  13.                 // 1秒后显示  
  14.                 window.setTimeout("updateTime(" + (60 - i) + ")", i * 1000);  
  15.             }  
  16.         }else{  
  17.             alert("请输入正确的手机号码");  
  18.             phone.focus();  
  19.               
  20.         }  
  21.     }  
  22.       
  23.     function updateTime(i){  
  24.         // setTimeout传多个参数到function有点麻烦,只能重新获取对象  
  25.         var obj = document.getElementById("validationCode");  
  26.         if(i > 0){  
  27.             obj.innerHTML  = "距离下次获取还有" + i + "秒";  
  28.             obj.disabled = true;  
  29.         }else{  
  30.             obj.innerHTML = "获取验证码";  
  31.             obj.disabled = false;  
  32.         }  
  33.     }  
  34.       
  35.     function checkTime(){  
  36.         var sendCodeTime = <%=(Long)session.getAttribute("SEND_CODE_TIME")%>;  
  37.         if(sendCodeTime){  
  38.             var nowTime = new Date().getTime();  
  39.             var flag = Math.floor((nowTime - sendCodeTime)/1000);  
  40.             if(flag < 60){  
  41.                 var end = 60 - flag;  
  42.                 // 进页面马上开始,选i为0  
  43.                 for (var i = 0; i <= end; i++) {  
  44.                     window.setTimeout("updateTime(" + (end - i) + ")", i * 1000);  
  45.                 }  
  46.             }  
  47.         }  
  48.     }  
  49.       
  50.     function doValidation(){  
  51.         if(validateFormValidateor.form()){  
  52.             $("#validateForm").ajaxSubmit({  
  53.                 success:function(data){  
  54.                     if(data == "success") {  
  55.                         alert("验证成功");  
  56.                     }else{  
  57.                         alert("验证失败");  
  58.                     }  
  59.                 }  
  60.              });  
  61.         }  
  62.     }  
  63.       
  64.     var validateFormValidateor;  
  65.     $().ready(function(){  
  66.         validateFormValidateor = $("#validateForm").validate({  
  67.             rules:{  
  68.                 phone:{  
  69.                     required:true,  
  70.                     number:true,  
  71.                     minlength:11  
  72.                 },  
  73.                 code:"required"  
  74.             },  
  75.             messages:{  
  76.                 phone:{  
  77.                     required: "请输入手机号码",  
  78.                     number: "只能输入数字",  
  79.                     minlength: "手机号码为11位"  
  80.                 }  
  81.             }  
  82.         });  
  83.     });  
  84. </script>  

 

  1. @Controller  
  2. @RequestMapping("/phone")  
  3. public class PhoneController {  
  4.       
  5.     private static final String VALIDATE_PHONE_CODE = "VALIDATE_PHONE_CODE";  
  6.     private static final String VALIDATE_PHONE = "VALIDATE_PHONE";  
  7.     private static final String SEND_CODE_TIME = "SEND_CODE_TIME";  
  8.       
  9.     @RequestMapping("index.html")  
  10.     protected String index() {  
  11.         return "phone/list";  
  12.     }  
  13.       
  14.     @RequestMapping("sendCode.html")  
  15.     @ResponseBody  
  16.     protected void sendCode(String phone, HttpServletRequest request) throws HttpException, IOException {  
  17.         StringBuilder code = new StringBuilder();  
  18.         Random random = new Random();  
  19.         // 6位验证码  
  20.         for (int i = 0; i < 6; i++) {  
  21.             code.append(String.valueOf(random.nextInt(10)));  
  22.         }  
  23.         HttpSession session = request.getSession();  
  24.         session.setAttribute(VALIDATE_PHONE, phone);  
  25.         session.setAttribute(VALIDATE_PHONE_CODE, code.toString());  
  26.         session.setAttribute(SEND_CODE_TIME, new Date().getTime());  
  27.         String smsText = "验证码:" + code;  
  28.         System.out.println("手机号:" + phone + ", " + smsText);  
  29.         SendMsg_webchinese.sendMessage(phone, smsText);  
  30.     }  
  31.       
  32.     @RequestMapping("validate.html")  
  33.     @ResponseBody  
  34.     protected String validate(HttpServletRequest request){  
  35.         HttpSession session = request.getSession();  
  36.         String code = (String) session.getAttribute(VALIDATE_PHONE_CODE);  
  37.         String phone = (String) session.getAttribute(VALIDATE_PHONE);  
  38.         String inputCode = request.getParameter("code");  
  39.         String inputPhone = request.getParameter("phone");  
  40.         if(phone.equals(inputPhone) && code.equalsIgnoreCase(inputCode)){  
  41.             return "success";  
  42.         }else{  
  43.             return "failure";  
  44.         }  
  45.     }  
  46. }  

 

  1. import java.io.IOException;  
  2. import java.io.UnsupportedEncodingException;  
  3.   
  4. import org.apache.commons.httpclient.Header;  
  5. import org.apache.commons.httpclient.HttpClient;  
  6. import org.apache.commons.httpclient.HttpException;  
  7. import org.apache.commons.httpclient.NameValuePair;  
  8. import org.apache.commons.httpclient.methods.PostMethod;  
  9.   
  10. public class SendMsg_webchinese {  
  11.   
  12.     public static final String UID = "123";  
  13.     public static final String KEY = "123456";  
  14.     public static final String SMS_SEND_URI = "http://utf8.sms.webchinese.cn";  
  15.     public static final String SMS_NUM_URI = "http://sms.webchinese.cn/web_api/SMS/?Action=SMS_Num";  
  16.       
  17.     // 发送短信  
  18.     public static int sendMessage(String phone, String smsText) throws HttpException, IOException{  
  19.         PostMethod post = new PostMethod(SMS_SEND_URI);  
  20.         NameValuePair[] data = { new NameValuePair("Uid", UID),  
  21.                 new NameValuePair("Key", KEY),  
  22.                 new NameValuePair("smsMob", phone),  
  23.                 new NameValuePair("smsText", smsText) };  
  24.         String result = executeMethod(post, data);  
  25.         System.out.println("发送短信数量:" + result + ",手机号:" + phone + "信息:" + smsText);  
  26.         post.releaseConnection();  
  27.         return Integer.parseInt(result);  
  28.     }  
  29.       
  30.     // 获取短信数量  
  31.     public static int smsNum() throws UnsupportedEncodingException, IOException{  
  32.         PostMethod post = new PostMethod(SMS_NUM_URI);  
  33.         NameValuePair[] data = { new NameValuePair("Uid", UID), new NameValuePair("Key", KEY) };  
  34.         String result = executeMethod(post, data);  
  35.         System.out.println("短信数量:" + result);  
  36.         post.releaseConnection();  
  37.         return Integer.parseInt(result);  
  38.     }  
  39.       
  40.     private static String executeMethod(PostMethod post, NameValuePair[] data) throws HttpException, IOException{  
  41.         post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");  
  42.         post.setRequestBody(data);  
  43.         HttpClient client = new HttpClient();  
  44.         client.executeMethod(post);  
  45.         Header[] headers = post.getResponseHeaders();  
  46.         int statusCode = post.getStatusCode();  
  47.         System.out.println("statusCode:" + statusCode);  
  48.         for (Header h : headers) {  
  49.             System.out.println(h.toString());  
  50.         }  
  51.         return new String(post.getResponseBodyAsString().getBytes("utf-8"));  
  52.     }  
  53. }  


短信发送的包可以在参考里的短信发送连接里下,UID和KEY是在里面注册的。

springMVC中web短信验证码校验<非原创>

标签:

原文地址:http://www.cnblogs.com/wangdi0827/p/4506521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!