国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Spring MVC 中 短信驗(yàn)證碼功能的實(shí)現(xiàn)方法

Spring MVC 中 短信驗(yàn)證碼功能的實(shí)現(xiàn)方法

2020-06-19 11:36chendongj JAVA教程

短信驗(yàn)證功能在各個(gè)網(wǎng)站應(yīng)用都非常廣泛,那么在springmvc中如何實(shí)現(xiàn)短信驗(yàn)證碼功能呢?今天小編抽時(shí)間給大家介紹下Spring MVC 中 短信驗(yàn)證碼功能的實(shí)現(xiàn)方法,一起看看吧

在外部網(wǎng)站中短信的驗(yàn)證很有必要,比如在實(shí)現(xiàn)注冊(cè)、驗(yàn)證用戶信息等的情況下。在SpringMVC中的實(shí)現(xiàn)如下:

短信接口

短信接口,有些企業(yè)會(huì)購(gòu)買的有移動(dòng)的短信平臺(tái)接口。如果是個(gè)人或者是小企業(yè)可以使用一些云服務(wù)的。比如百度的API Store上面的。

我使用的是:http://apistore.baidu.com/apiworks/servicedetail/1018.html

當(dāng)然短信接口肯定都是要付費(fèi)的,而且是基于模板的,具體的使用說明可以看這個(gè)網(wǎng)址里面的使用說明。

前端界面

前端的界面,可能如下,點(diǎn)擊獲取驗(yàn)證碼,然后按鈕變?yōu)榛疑⑶业褂?jì)時(shí)。(手機(jī)號(hào)是我的~~)

Spring MVC 中 短信驗(yàn)證碼功能的實(shí)現(xiàn)方法

HTML代碼就不寫了,JS如下:vailidationCode是獲取驗(yàn)證碼按鈕的ID。phone是手機(jī)號(hào)碼的ID,手機(jī)號(hào)碼只是簡(jiǎn)單的驗(yàn)證了,如果是要更精確,使用正則,其中的url的sendSms是后臺(tái)的springMVC的路徑。

?
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
$("#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("請(qǐng)輸入合法的手機(jī)號(hào)");
}
});
var countdown=60;
function updateButtonStatus(){
var phone = $("#validationCode");
if (countdown == 0) {
phone.attr("disabled","false");
phone.val("免費(fèi)獲取驗(yàn)證碼");
countdown = 60;
return;
} else {
phone.attr("disabled","true");
phone.val("重新發(fā)送(" + countdown + ")");
countdown--;
}
setTimeout(function() {
updateButtonStatus() }
,1000)
}

后端代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RequestMapping(value = "/sendSms")
@ResponseBody
public String sendSMS(@RequestParam("phone") String phone, HttpServletRequest request){
StringBuilder code = new StringBuilder();
Random random = new Random();
// 生成6位驗(yàn)證碼
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 = "您的驗(yàn)證碼是:"+code;
SMSUtil.send(phone,smsText);
return "success";
}

其中的SMSUtil是封裝的上面的短信接口的發(fā)送類。參考如下,其中的API_KEY改成自己的。

?
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
public class SMSUtil {
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");
// 填入apikey到HTTP header
connection.setRequestProperty("apikey",API_KEY);
connection.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;
}
}

前臺(tái)的表單提交前還需要使用ajax做一下表單的驗(yàn)證,驗(yàn)證一下驗(yàn)證碼是否正確:

?
1
2
3
4
5
6
7
8
9
10
11
12
@RequestMapping("/validate")
@ResponseBody
protected 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";
}
}

以上所述是小編給大家介紹的Spring MVC 中 短信驗(yàn)證碼功能的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

原文鏈接:https://my.oschina.net/chendongj/blog/753785

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人网av| 国产精品对白一区二区三区 | 一区二区不卡视频 | 一区二区在线 | 91免费视频网站 | 久久国内| 欧美一区二区三区精品 | 亚洲 成人 av | 伊人色私人影院蜜桃va | a级在线免费观看 | 日韩在线中文字幕 | 日韩三级电影免费观看 | 久久久久久成人 | 夜夜草视频 | 一区二区三区久久久 | 亚洲一区精品在线 | 91精品国产91久久久久久最新 | 91精品一久久香蕉国产线看观看新通道出现 | 久久亚洲精品中文字幕 | 日韩欧美三级在线观看 | 人人99| 青青草91在线视频 | 黄色视屏在线免费观看 | 人妖天堂狠狠ts人妖天堂狠狠 | 野花国产精品入口 | 免费看一区二区三区 | 黄瓜av| 亚洲国产欧美一区二区三区丁香婷 | 精品福利视频网站 | 自拍视频网站 | 成人综合网站 | 亚洲一区在线日韩在线深爱 | 日本欧美一区二区 | 午夜精品久久久久久久久久久久 | 国产精品一区一区三区 | 亚洲成人免费网站 | 日韩成人免费中文字幕 | 极品久久 | 看av网站| 在线观看特色大片免费网站 | 一级黄色片看看 |