小編給大家分享一下java實(shí)現(xiàn)短信驗(yàn)證碼5分鐘有效時(shí)間的示例,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,先為江干等服務(wù)建站,江干等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為江干企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
具體內(nèi)容如下
實(shí)現(xiàn)一個(gè)發(fā)送短信驗(yàn)證碼的請(qǐng)求,要求5分鐘之內(nèi)重復(fù)請(qǐng)求,返回同一個(gè)驗(yàn)證碼。
網(wǎng)上可找到幾種方案:
如,存儲(chǔ)數(shù)據(jù)庫(kù)或緩存中。實(shí)現(xiàn)起來(lái)比較麻煩,舍棄;
另一種方式即本例,使用session存儲(chǔ)。其他方式,暫未進(jìn)一步了解。
實(shí)現(xiàn)步驟:(springmvc)
1、controller中,獲取session對(duì)象,取code,取不到新生成,并存儲(chǔ)session中;
2、單手機(jī)號(hào)發(fā)送量,判斷并 +1 記入數(shù)據(jù)庫(kù);
3、Timer定時(shí)器,設(shè)置新線程延時(shí)執(zhí)行TimerTask任務(wù)(刪除code)
@RequestMapping(value = "sendMessage",method = RequestMethod.GET) public Object sendMessage(final HttpServletRequest request){ String phone=request.getParameter("phone"); int times=userService.messageSendToday(phone); //二次驗(yàn)證,單個(gè)手機(jī)號(hào)每日發(fā)送上限 if(times <= MAX_PER_DAY){ String checkCode=GenerateRandomCode.createRandomNumber(6); final HttpSession httpSession=request.getSession(); httpSession.setAttribute("checkCode",checkCode); CheckCodeMessage checkCodeMessage=new CheckCodeMessage(phone,checkCode); try { HttpSender.batchSend(checkCodeMessage); //TimerTask實(shí)現(xiàn)5分鐘后從session中刪除checkCode final Timer timer=new Timer(); timer.schedule(new TimerTask() { @Override public void run() { httpSession.removeAttribute("checkCode"); System.out.println("checkCode刪除成功"); timer.cancel(); } },5*60*1000); } catch (Exception e) { e.printStackTrace(); } return "redirect:/index.jsp"; } }
Timer定時(shí)任務(wù):
//TimerTask實(shí)現(xiàn)5分鐘后從session中刪除checkCode final Timer timer=new Timer(); timer.schedule(new TimerTask() { @Override public void run() { httpSession.removeAttribute("checkCode"); System.out.println("checkCode刪除成功"); timer.cancel(); } },5*60*1000);
看完了這篇文章,相信你對(duì)“java實(shí)現(xiàn)短信驗(yàn)證碼5分鐘有效時(shí)間的示例”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文題目:java實(shí)現(xiàn)短信驗(yàn)證碼5分鐘有效時(shí)間的示例
鏈接分享:http://www.chinadenli.net/article36/iepdsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、虛擬主機(jī)、定制網(wǎng)站、全網(wǎng)營(yíng)銷(xiāo)推廣、App設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)