上周由于其他的工作繁忙,都沒(méi)時(shí)間來(lái)寫(xiě)博客,這不一有時(shí)間,就馬上來(lái)寫(xiě)最近實(shí)現(xiàn)的性功能了。
這篇主要講的是怎么讓短信驗(yàn)證碼具有時(shí)效性,我手上的項(xiàng)目要求的時(shí)效性是5分鐘,那么話不多說(shuō),我就開(kāi)始碼代碼了~這里主要還用到了第三方的短信接口,互億無(wú)線短信平臺(tái)。www.ihuyi.com
實(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)
publicObjectsendMessage(finalHttpServletRequestrequest){
Stringphone=request.getParameter("phone");
inttimes=userService.messageSendToday(phone);//二次驗(yàn)證,單個(gè)手機(jī)號(hào)每日發(fā)送上限
if(times<=MAX_PER_DAY){
StringcheckCode=GenerateRandomCode.createRandomNumber(6);
finalHttpSessionhttpSession=request.getSession();
httpSession.setAttribute("checkCode",checkCode);
CheckCodeMessagecheckCodeMessage=newCheckCodeMessage(phone,checkCode);
try{
HttpSender.batchSend(checkCodeMessage);
//TimerTask實(shí)現(xiàn)5分鐘后從session中刪除checkCode
finalTimertimer=newTimer();
timer.schedule(newTimerTask(){
@Override
publicvoidrun(){
httpSession.removeAttribute("checkCode");
System.out.println("checkCode刪除成功");
timer.cancel();
}
},5*60*1000);
}catch(Exceptione){
e.printStackTrace();
}
return"redirect:/index.jsp";
}
}