输入错误 5次 就锁定不让输入 第二天才能再输入

public void getLockState(InvestorBaseAccountEntity account, InvestorBaseAccountRedisInfo accountRedis, boolean isLogin){

  if (null != accountRedis.getLockTime()) {
    // 不是同一天,更新用户登录错误次数,清零
    if (!DateUtil.same(DateUtil.getSqlDate(), accountRedis.getLockTime())){
      accountRedis.setPwdErrorTimes(0);
      accountRedis.setLockTime(DateUtil.getSqlDate());
      investorBaseAccountRedisUtil.set(redis, account.getOid(), accountRedis);
    }
  }

  if (accountRedis.getPwdErrorTimes() > 4) {
    if (isLogin) {
      throw new AMPException("密码连续输入错误超过五次,账号已被锁定24小时!");
    } else {
      throw new AMPException("您的账号已被锁定,不能进行此操作!");
    }
  }
}

//   限定条件为      同一天  +  5次错误          

        判断是逻辑分析   :                          锁定时间 与 当前时间                                   同一天     或者      并非同一天

                                      +

                                      5次错误时

                                同一天 + 5    限定  ,    非同一天  + 5   并不限定     

                                其实我们没注意到的一点是:(只要是不是当天, 也就是 到了今天以后      我们输入错误的次数   就应该清零,输入的的错误值是今天输入的 ,不应该影响到明天或以后,      所以锁定的条件 就是        今天   且    错了5次         只有是今天 也就是同一天(sameday的判断过了以后  才存在  判断  错误次数这么一说) )

注意:    此处补充            如果输入正确  应该            将 输入错误次数  设置为     0;

完整的逻辑代码:

// 获取锁定状态
this.getLockState(account, accountRedis, true);

if (PwdUtil.checkPassword(req.getUserPwd(), account.getUserPwd(), account.getSalt())) {
    // 更新用户登录错误次数,清零
    accountRedis.setPwdErrorTimes(0);
    this.updateAccountRedis(account.getOid(), accountRedis);
    return account.getOid();
} else {
    // 错误次数累计
    int pwdErrorTimes = accountRedis.getPwdErrorTimes() + 1;
   if(pwdErrorTimes == 5){
    // 设置锁定时间
      accountRedis.setLockTime(DateUtil.getSqlDate());
    }
      // 更新用户登录错误次数
      accountRedis.setPwdErrorTimes(pwdErrorTimes);
      this.updateAccountRedis(account.getOid(), accountRedis);
    if(pwdErrorTimes == 5){
      logger.info("用户:{},密码输入错误:5次,时间:{}", account.getPhoneNum(), DateUtil.getSqlDate());
      throw GHException.getException("密码连续输入错误超过五次,账号已被锁定24小时!");
    }
      logger.info("用户:{},密码输入错误:{}次,时间:{}", account.getPhoneNum(), pwdErrorTimes, DateUtil.getSqlDate());
           throw new GHException("登录名和密码不匹配,连续输错超过5次账号当天将会被锁定,剩余" + (5- pwdErrorTimes) + "次机会");
}

原文地址:https://www.cnblogs.com/lize1215/p/8487418.html