小編給大家分享一下java實現(xiàn)短信驗證碼5分鐘有效時間的示例,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,先為江干等服務(wù)建站,江干等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為江干企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
具體內(nèi)容如下
實現(xiàn)一個發(fā)送短信驗證碼的請求,要求5分鐘之內(nèi)重復(fù)請求,返回同一個驗證碼。
網(wǎng)上可找到幾種方案:
如,存儲數(shù)據(jù)庫或緩存中。實現(xiàn)起來比較麻煩,舍棄;
另一種方式即本例,使用session存儲。其他方式,暫未進(jìn)一步了解。
實現(xiàn)步驟:(springmvc)
1、controller中,獲取session對象,取code,取不到新生成,并存儲session中;
2、單手機號發(fā)送量,判斷并 +1 記入數(shù)據(jù)庫;
3、Timer定時器,設(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); //二次驗證,單個手機號每日發(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實現(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定時任務(wù):
//TimerTask實現(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);
看完了這篇文章,相信你對“java實現(xiàn)短信驗證碼5分鐘有效時間的示例”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文題目:java實現(xiàn)短信驗證碼5分鐘有效時間的示例
鏈接分享:http://jinyejixie.com/article36/iepdsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、虛擬主機、定制網(wǎng)站、全網(wǎng)營銷推廣、App設(shè)計、手機網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)