有關阿里云通信短信服務驗證碼的發送,請參考我的另一篇文章 springboot實現阿里云通信短信服務有關短信驗證碼的發送功能
思路
用戶輸入手機號后,點擊按鈕獲取驗證碼。并設置冷卻時間,防止用戶頻繁點擊。
后臺生成驗證碼并發送到用戶手機上,根據驗證碼、時間及一串自定義秘鑰生成md5值,并將時間也傳回到前端。
用戶輸入驗證碼后,將驗證碼和時間傳到后臺。后臺先用當前時間減去前臺傳過來的時間驗證是否超時。如果沒有超時,就用用戶輸入的驗證碼 + 時間 + 自定義秘鑰生成md5值與之前的md5值比較,如果相等則驗證碼校驗通過,如果不等則說明驗證碼輸入錯誤校驗失敗。
原理有點像解方程:
xyz經過一種不可逆運算得到a,將y和a傳給用戶,z后臺保留,用戶填寫x1后,將x1 y a傳回后臺,后臺再用x1 y z經過不可逆運算得到a1,如果a1和a相等,則驗證碼校驗通過。
前端的實現
本例基于bootstrap,html代碼中有bootstrap樣式。如果你不想用bootstrap,可以將class樣式去掉。效果如圖所示。
html代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<div class = "form-group has-feedback" > <input type= "tel" class = "form-control" id= "phone" placeholder= "請輸入手機號" maxlength= 11 > <span class = "glyphicon glyphicon-earphone form-control-feedback" ></span> </div> <div class = "row" > <div class = "col-xs-6 pull_left" > <div class = "form-group" > <input class = "form-control" id= "msg_num" placeholder= "請輸入驗證碼" > </div> </div> <div class = "col-xs-6 pull_center" > <div class = "form-group" > <input type= "button" class = "btn btn-block btn-flat" id= "verify_refresh" onclick= "getmsgnum(this)" value= "免費獲取驗證碼" > </div> </div> </div> <div class = "col-xs-12 pull_center" > <button type= "button" class = "btn btn-block btn-flat" onclick= "validatenum()" >驗證</button> </div> |
js代碼(基于jquery)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
var messagedata; /** * 獲取驗證碼 * @param that */ function getmsgnum(that) { var phonenumber = $( '#phone' ).val(); setbuttonstatus(that); // 設置按鈕倒計時 var obj = { phonenumber: phonenumber }; $.ajax({ url: httpurl + '/sendmsg' , // 后臺短信發送接口 type: 'post' , datatype: 'json' , contenttype: "application/json" , async: false , //false 同步 data: json.stringify(obj), xhrfields: { withcredentials: true }, success: function (result) { if (result.code == '200' ) { messagedata = result.data; } else { alert( "錯誤碼:" + data.code + " 錯誤信息:" + data.message); } }, error: function (xmlhttprequest, textstatus, errorthrown) { console.log(xmlhttprequest.status); console.log(xmlhttprequest.readystate); console.log(textstatus); } }); } /** * 設置按鈕狀態 */ function setbuttonstatus(that) { if (wait == 0 ) { that.removeattribute( "disabled" ); that.value= "免費獲取驗證碼" ; wait = 60 ; } else { that.setattribute( "disabled" , true ); that.value=wait+ "秒后可以重新發送" ; wait--; settimeout(function() { setbuttonstatus(that) }, 1000 ) } } /** * 注冊按鈕 */ function validatenum() { var data = { msgnum: inputmsgnum, tamp: messagedata.tamp, hash: messagedata.hash }; $.ajax({ url: httpurl + '/validatenum' , // 驗證接口 type: 'post' , datatype: 'json' , contenttype: "application/json" , data: json.stringify(data), async: false , //false 同步 success: function (data) { //業務處理 }, error: function (xmlhttprequest, textstatus, errorthrown) { console.log(xmlhttprequest.status); console.log(xmlhttprequest.readystate); console.log(textstatus); } }); } |
其中setbuttonstatus()方法用于設置按鈕冷卻狀態。效果如下圖
后臺的實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
private static final string key = "abc123" ; // key為自定義秘鑰 @requestmapping (value = "/sendmsg" , method = requestmethod.post, headers = "accept=application/json" ) public map<string, object> sendmsg( @requestbody map<string,object> requestmap) { string phonenumber = requestmap.get( "phonenumber" ).tostring(); string randomnum = commonutils.createrandomnum( 6 ); // 生成隨機數 simpledateformat sf = new simpledateformat( "yyyymmddhhmmss" ); calendar c = calendar.getinstance(); c.add(calendar.minute, 5 ); string currenttime = sf.format(c.gettime()); // 生成5分鐘后時間,用戶校驗是否過期 sengmsg(); //此處執行發送短信驗證碼方法 string hash = md5utils.getmd5code(key + "@" + currenttime + "@" + randomnum); //生成md5值 map<string, object> resultmap = new hashmap<>(); resultmap.put( "hash" , hash); resultmap.put( "tamp" , currenttime); return resultmap; //將hash值和tamp時間返回給前端 } @requestmapping (value = "/validatenum" , method = requestmethod.post, headers = "accept=application/json" ) public map<string, object> validatenum( @requestbody map<string,object> requestmap) { string requesthash = requestmap.get( "hash" ).tostring(); string tamp = requestmap.get( "tamp" ).tostring(); string msgnum = requestmap.get( "msgnum" ).tostring(); string hash = md5utils.getmd5code(key + "@" + tamp + "@" + msgnum); if (tamp.compareto(currenttime) > 0 ) { if (hash.equalsignorecase(requesthash)){ //校驗成功 } else { //驗證碼不正確,校驗失敗 } } else { // 超時 } } |
總結
以上所述是小編給大家介紹的springboot實現短信驗證碼校驗方法思路詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/colton_null/article/details/77283545