zoukankan      html  css  js  c++  java
  • Shiro密码重试次数限制

    如在 1 个小时内密码最多重试 5 次,如果尝试次数超过 5 次就锁定 1 小时,1 小时后可再次重试,如果还是重试失败,可以锁定如 1 天,以此类推,防止密码被暴力破解。我们通过继承 HashedCredentialsMatcher,且使用 Ehcache 记录重试次数和超时时间。

    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
           String username = (String)token.getPrincipal();
            //retry count + 1
            Element element = passwordRetryCache.get(username);
            if(element == null) {
                element = new Element(username , new AtomicInteger(0));
                passwordRetryCache.put(element);
            }
            AtomicInteger retryCount = (AtomicInteger)element.getObjectValue();
            if(retryCount.incrementAndGet() > 5) {
                //if retry count > 5 throw
                throw new ExcessiveAttemptsException();
            }
            boolean matches = super.doCredentialsMatch(token, info);
            if(matches) {
                //clear retry count
                passwordRetryCache.remove(username);
            }
            return matches;
    }

    如上代码逻辑比较简单,即如果密码输入正确清除 cache 中的记录;否则 cache 中的重试次数 +1,如果超出 5 次那么抛出异常表示超出重试次数了。


    QQ群:785071190
    查看原文:http://www.coder306.cn/?p=209
  • 相关阅读:
    pyecharts包学习笔记
    敏捷测试关键成功因素
    JMeter—常见问题(十四)
    性能测试面试题
    python-Tkinter整理总结
    JMeter—系统性能分析思路(十三)
    JMeter—监听器(十二)
    JMeter—断言(十一)
    yii2.0 的数据的 增
    Windows下安装 使用coreseek
  • 原文地址:https://www.cnblogs.com/coder306/p/13087672.html
Copyright © 2011-2022 走看看