2018-07-17 13:49:55 1504瀏覽
在現(xiàn)如今,大多數(shù)網(wǎng)站都需要用到網(wǎng)站注冊手機短信驗證功能,最近有很多同學(xué)反映不會Java開發(fā)實現(xiàn)短信驗證,今天扣丁學(xué)堂小編讓Java培訓(xùn)老師給大家整理了一篇關(guān)于Java實現(xiàn)短信驗證碼5分鐘有效時間,下面我們一起來看一下吧。如存儲數(shù)據(jù)庫或緩存中。實現(xiàn)起來比較麻煩,舍棄;另一種方式即本例,使用session存儲。其他方式,暫未進一步了解。
@RequestMapping(value="sendMessage",method=RequestMethod.GET) publicObjectsendMessage(finalHttpServletRequestrequest){ Stringphone=request.getParameter("phone"); inttimes=userService.messageSendToday(phone);//二次驗證,單個手機號每日發(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實現(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"; } }
//TimerTask實現(xiàn)5分鐘后從session中刪除checkCode finalTimertimer=newTimer(); timer.schedule(newTimerTask(){ @Override publicvoidrun(){ httpSession.removeAttribute("checkCode"); System.out.println("checkCode刪除成功"); timer.cancel(); } },5*60*1000);
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>