实现短信验证码验证时,应判断短信验证码是否已经被使用过、短信验证码是否正确、短信验证码是否超时等。

    1. public int doControl() throws SsException {
    2. try {
    3. String sms_input = (String) mapValue.get("sms_yzm");
    4. String errMsg = (String) mapValue.get("respmsg");
    5. String sms_yzm = priDataCache.getParam("sms_yzm");
    6. String sms_time = priDataCache.getParam("sms_yzm_time");
    7. boolean isBeyondCount = false;
    8. //检查是否获取到短信验证码
    9. if (sms_yzm == null || sms_time == null || sms_time.equals("") || sms_yzm.equals("")) {
    10. priDataCache.setParam("respcode", "m2019");
    11. priDataCache.setParam("respmsg", "未获取短信验证码!");
    12. return -1;
    13. }
    14. //短信验证码超时检查
    15. if (System.currentTimeMillis() - 180000 > Long.parseLong(sms_time)) {//三分钟
    16. priDataCache.setParam("respcode", "m2020");
    17. priDataCache.setParam("respmsg", "短信验证码已经超时,请重新获取");
    18. return -1;
    19. }
    20. //检查短信验证码是否已被使用
    21. if (sms_input != null && sms_input.equals(sms_yzm)) {
    22. priDataCache.setParam("sms_yzm", "");
    23. priDataCache.setParam("sms_yzm_time", "");
    24. }
    25. //判断短信的错误尝试次数
    26. if (sms_input != null) {
    27. int msgCount = iBaseDao.queryForInt("customer.countMsg", param);
    28. if (msgCount > 10) {
    29. isBeyondCount = true;
    30. }
    31. if (isBeyondCount) {
    32. TransUtil.buildResponseMessage(AppConstants.RspCode_FAIL, "短信验证码在一天之内不允许超过10次", rst);
    33. return rst;
    34. } else {
    35. priDataCache.setParam("respcode", "m2021");
    36. priDataCache.setParam("respmsg", errMsg);
    37. return -1;
    38. }
    39. }catch(Exception ex){
    40. Log.getInstance().error(logId, ex.getMessage(), ex);
    41. throw new SsException("m2022", "验证码输入不正确" + ex.toString());
    42. }
    43. return 0;
    44. }