实现短信验证码验证时,应判断短信验证码是否已经被使用过、短信验证码是否正确、短信验证码是否超时等。
public int doControl() throws SsException {
try {
String sms_input = (String) mapValue.get("sms_yzm");
String errMsg = (String) mapValue.get("respmsg");
String sms_yzm = priDataCache.getParam("sms_yzm");
String sms_time = priDataCache.getParam("sms_yzm_time");
boolean isBeyondCount = false;
//检查是否获取到短信验证码
if (sms_yzm == null || sms_time == null || sms_time.equals("") || sms_yzm.equals("")) {
priDataCache.setParam("respcode", "m2019");
priDataCache.setParam("respmsg", "未获取短信验证码!");
return -1;
}
//短信验证码超时检查
if (System.currentTimeMillis() - 180000 > Long.parseLong(sms_time)) {//三分钟
priDataCache.setParam("respcode", "m2020");
priDataCache.setParam("respmsg", "短信验证码已经超时,请重新获取");
return -1;
}
//检查短信验证码是否已被使用
if (sms_input != null && sms_input.equals(sms_yzm)) {
priDataCache.setParam("sms_yzm", "");
priDataCache.setParam("sms_yzm_time", "");
}
//判断短信的错误尝试次数
if (sms_input != null) {
int msgCount = iBaseDao.queryForInt("customer.countMsg", param);
if (msgCount > 10) {
isBeyondCount = true;
}
if (isBeyondCount) {
TransUtil.buildResponseMessage(AppConstants.RspCode_FAIL, "短信验证码在一天之内不允许超过10次", rst);
return rst;
} else {
priDataCache.setParam("respcode", "m2021");
priDataCache.setParam("respmsg", errMsg);
return -1;
}
}catch(Exception ex){
Log.getInstance().error(logId, ex.getMessage(), ex);
throw new SsException("m2022", "验证码输入不正确" + ex.toString());
}
return 0;
}