模拟后台接收短信验证码
生活随笔
收集整理的這篇文章主要介紹了
模拟后台接收短信验证码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
struts2 Action中
//模擬生成6為隨機驗證碼
public String createCode(){String code=null;Random random=new Random();int[] rand=new int[6];int randInt = 0;String randStr="";String temp;for(int i=0;i<6;i++){randInt = random.nextInt(10);rand[i]=randInt;temp=rand[i]+"";randStr=randStr+temp;}code=randStr;sessionCode.setAttribute("code", code);sessionCode.setAttribute("time", System.currentTimeMillis());System.out.println(code);return null;}
//首先判斷會話是否獲取短信驗證碼
//設置驗證碼失效時間,通過System.currentTimeMillis();獲取訪問各自接口的毫秒數,做差與30000(ms)作比較,超時移除。
//其中(30000ms)為本測試接口設置
//的短信驗證碼生效時間,向頁面輸入的驗證碼"codeInput="與獲取的驗證碼進行比較
private static Logger logger = Logger.getLogger ( XxAction.class.getName () );
public String testSession(){if(sessionCode.getAttribute("code")!=null){long early=(long) sessionCode.getAttribute("time");logger.info(early);long nowTime=System.currentTimeMillis();logger.info(nowTime);if(nowTime-early<30000){if(sessionCode.getAttribute("code").equals(codeInput)){logger.info(codeInput);}}else{sessionCode.removeAttribute("code");sessionCode.removeAttribute("time");logger.info("code已過期,請重新獲取!");}}else{logger.info("請重新獲取!");}return null;}
?
總結
以上是生活随笔為你收集整理的模拟后台接收短信验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shader入门精要读书笔记40 素描风
- 下一篇: WEB安全实战(带靶场)–暴力破解