模拟后台接收短信验证码

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;

	}


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值