您的位置:首页 > 其它

模拟后台接收短信验证码

2016-05-24 13:07 495 查看
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: