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次将会 出现错误次数过多异常
测试结果是没问题的,不在黏贴

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值