zoukankan      html  css  js  c++  java
  • shiro 错误登陆次数限制

    第一步:在spring-shiro.xml 中配置缓存管理器和认证匹配器

    <!-- 缓存管理器 使用Ehcache实现 -->
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
    <property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />
    </bean>
    <!-- 凭证匹配器 -->
    <bean id="credentialsMatcher"
    class="com.rongke.web.shiro.MyHashedCredentialsMatcher">
    <constructor-arg ref="cacheManager" />
    <property name="hashAlgorithmName" value="md5" />
    <property name="hashIterations" value="3" />
    <property name="storedCredentialsHexEncoded" value="true" />
    </bean>
    在自定义的realm中引入匹配器
    <bean id="codeRealm" class="com.rongke.web.shiro.AdminPasswordRealm">
    <property name="credentialsMatcher" ref="credentialsMatcher"/>
    </bean>
    第二部 引入org.apache.shiro.cache.ehcache.EhCacheManager 所需的jar
    <dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-ehcache</artifactId>
    <version>1.2.4</version>
    </dependency>
    第三步 编辑ehcache.xml 文件
    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache name="shirocache" >

    <diskStore path="java.io.tmpdir" />
    <defaultCache
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="120"
    timeToLiveSeconds="120"
    overflowToDisk="true"
    />
    <!-- 登录记录缓存 锁定10分钟 -->
    <cache name="passwordRetryCache" eternal="false"
    maxEntriesLocalHeap="2000"
    timeToIdleSeconds="3600" timeToLiveSeconds="0" overflowToDisk="false"
    statistics="true">
    </cache>

    <!--<cache name="authorizationCache" eternal="false"-->
    <!--timeToIdleSeconds="3600" timeToLiveSeconds="0" overflowToDisk="false"-->
    <!--statistics="true">-->
    <!--</cache>-->

    <!--<cache name="authenticationCache" eternal="false"-->
    <!--timeToIdleSeconds="3600" timeToLiveSeconds="0" overflowToDisk="false"-->
    <!--statistics="true">-->
    <!--</cache>-->

    <!--<cache name="shiro-activeSessionCache" eternal="false"-->
    <!--timeToIdleSeconds="3600" timeToLiveSeconds="0" overflowToDisk="false"-->
    <!--statistics="true">-->
    <!--</cache>-->

    </ehcache>

    第四步 编写直接的认证匹配类
    public class MyHashedCredentialsMatcher extends HashedCredentialsMatcher {

    private Cache<String, AtomicInteger> passwordRetryCache;
    public RetryLimitHashedCredentialsMatcher(CacheManager cacheManager) {
    passwordRetryCache = cacheManager.getCache("passwordRetryCache");
    }

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token,
    AuthenticationInfo info) {
    String username = (String) token.getPrincipal();
    // retry count + 1
    AtomicInteger retryCount = passwordRetryCache.get(username);
    if (retryCount == null) {
    retryCount = new AtomicInteger(0);
    passwordRetryCache.put(username, retryCount);
    }
    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;
    }


    }
    第五步测试
    刚开始启动没有启动成功 出现这样的错误
    nested exception is org.apache.shiro.cache.CacheException: net.sf.ehcache.config.InvalidConfigurationException: There is one error in your configuration: 
    * Cache 'passwordRetryCache' error: If your CacheManager has no maxBytesLocalHeap set, you need to either set maxEntriesLocalHeap or maxBytesLocalHeap at the Cache level
    由于ehcache.xml文件直接网上copy的,<cache></cache>缺少了maxEntriesLocalHeap 属性,添加上再次启动就ok了
    在 MyHashedCredentialsMatcher 设置了连续出错5次将会 出现错误次数过多异常
    测试结果是没问题的,不在黏贴
    
    
  • 相关阅读:
    linux/unix下 pid文件作用浅析
    gunicorn启动django时静态文件的加载
    分享30道Redis面试题,面试官能问到的我都找到了
    python利用mongodb上传图片数据 : GridFS 与 bson两种方式
    GridFS大文件的添加、获取、查看、删除
    你真的懂redis吗?
    mongoDB的复制集5----复制集安全(认证,用户,权限)
    MongoDB复制集安全认证
    MongoDB 用户名密码登录
    MongoDB 分片
  • 原文地址:https://www.cnblogs.com/prettrywork/p/10240104.html
Copyright © 2011-2022 走看看