校验登录次数是否超过限制

private Cache<String, AtomicInteger> loginRecordCache;

@Value(value = "${user.password.maxRetryCount}")
private String maxRetryCount;

@PostConstruct
public void init()
{
    loginRecordCache = cacheManager.getCache(ShiroConstants.LOGINRECORDCACHE);
}

public void validate(SysUser user, String password)
{
    String loginName = user.getLoginName();

    AtomicInteger retryCount = loginRecordCache.get(loginName);

    if (retryCount == null)
    {
        retryCount = new AtomicInteger(0);
        loginRecordCache.put(loginName, retryCount);
    }
    if (retryCount.incrementAndGet() > Integer.valueOf(maxRetryCount).intValue())
    {
        //AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", maxRetryCount)));
        throw new UserPasswordRetryLimitExceedException(Integer.valueOf(maxRetryCount).intValue());
    }

    if (!matches(user, password))
    {
        //AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.count", retryCount)));
        loginRecordCache.put(loginName, retryCount);
        throw new UserPasswordNotMatchException();
    }
    else
    {
        clearLoginRecordCache(loginName);
    }
}

public boolean matches(SysUser user, String newPassword)
{
    return user.getPassword().equals(encryptPassword(user.getLoginName(), newPassword, user.getSalt()));
}
发布了207 篇原创文章 · 获赞 23 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/stone_tomcate/article/details/103461672
今日推荐