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
  • 相关阅读:
    发起qq临时会话
    easyUI-textbox回车获取不到正确的textbox值问题
    Linq in条件查询
    常用js-API
    MVC4不支持EF6解决方案 && Nuget控制台操作说明
    JS报表打印分页CSS
    关于phpinfo页面展开的渗透
    基于phpmyadmin的攻击
    upload_labs靶场
    文件上传漏洞
  • 原文地址:https://www.cnblogs.com/coder306/p/13087672.html
Copyright © 2011-2022 走看看