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

shiro 错误登陆次数限制的更多相关文章

  1. Spring集成shiro做登陆认证

    一.背景 其实很早的时候,就在项目中有使用到shiro做登陆认证,直到今天才又想起来这茬,自己抽空搭了一个spring+springmvc+mybatis和shiro进行集成的种子项目,当然里面还有很 ...

  2. Shiro 自定义登陆、授权、拦截器

    Shiro 登陆.授权.拦截 按钮权限控制 一.目标 Maven+Spring+shiro 自定义登陆.授权 自定义拦截器 加载数据库资源构建拦截链 使用总结: 1.需要设计的数据库:用户.角色.权限 ...

  3. Spring与Shiro整合 登陆操作

    Spring与Shiro整合 登陆操作 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 编写登陆Controller方法  讲解: 首先,如果你登陆失败的时候,它会把你的异常信息丢到 ...

  4. redis 实现登陆次数限制

    title: redis-login-limitation 利用 redis 实现登陆次数限制, 注解 + aop, 核心代码很简单. 基本思路 比如希望达到的要求是这样: 在 1min 内登陆异常次 ...

  5. Fundebug 微信小程 BUG 监控插件更新至 1.2.1,优化错误上报次数的限制算法,新增 silentHttpHeader 配置选项

    摘要: 1.2.1优化错误上报次数的限制算法,新增silentHttpHeader配置选项,请大家及时更新哈! Fundebug提供专业的微信小程序 BUG 监控服务,可以第一时间为您捕获生存环境中小 ...

  6. linux系统查看某个用户错误登录次数

    pam_tally2 --user user_name 查看user_name用户的错误登录次数 pam_tally2 --user user_name --reset 清空user_name用户的错 ...

  7. 【BASIS系列】SAP 中查看account登陆次数及时间的情况

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[BASIS系列]SAP 中查看account登 ...

  8. Spring boot 入门(四):集成 Shiro 实现登陆认证和权限管理

    本文是接着上篇博客写的:Spring boot 入门(三):SpringBoot 集成结合 AdminLTE(Freemarker),利用 generate 自动生成代码,利用 DataTable 和 ...

  9. 分享知识-快乐自己:Shiro 退出登陆清空缓存实现

    shiro是一个被广泛使用的安全层框架,通过xml配置方式与spring无缝对接,用户的登陆/退出/权限控制/Cookie等管理系统基础功能交给shiro来管理. 一般,在JavaWEB管理平台系统时 ...

随机推荐

  1. spring boot Websocket(使用笔记)

    ​ 使用websocket有两种方式:1是使用sockjs,2是使用h5的标准.使用Html5标准自然更方便简单,所以记录的是配合h5的使用方法. 1.pom ​ 核心是@ServerEndpoint ...

  2. ueditor 回显html样式会直接出来,无效

    百度了好久,后面发现可以直接这样

  3. jQuery 源码学习 - 01 - 简洁的 $('...')

    首先贴上学习参考资料:[深入浅出jQuery]源码浅析--整体架构,备用地址:chokcoco/jQuery-. jQuery 库,js 开发的一个里程碑,它的出现,让网页开发者们告别荒蛮的上古时代, ...

  4. knnsearch

    转载:https://blog.csdn.net/bushixiaofan/article/details/27700299 K近邻算法是找到K个最近的邻居. IDX = knnsearch(X,Y) ...

  5. Jetson TX1刷机

    刷机流程 https://blog.csdn.net/c406495762/article/details/70786700 注意:教程中包含两步,首先安装Ubuntu系统,然后重启安装程序,安装其他 ...

  6. 初学tensorflow遇到的Error——UnrecognizedFlagError: Unknown command line flag 'f'

    最近在学习<tensorflow实战>时需要下载cifar10数据集,在cifar10目录下用到命令: import cifar10,cifar10_inputcifar10.maybe_ ...

  7. Leetcode 8 Two Pointers

    Two Pointers 1. 28. Implement strStr() 用 i 记录haystack偏移量,j 记录 needle 的偏移量. class Solution { public i ...

  8. Go语言中的方法和函数

    在C#或者Java里面我们都知道,一个Class是要包含成员变量和方法的,对于GO语言的Struct也一样,我们也可以给Struct定义一系列方法. 一.怎么定义一个方法? Go的方法是在函数前面加上 ...

  9. 【THUSC2017】【LOJ2979】换桌 线段树 网络流

    题目大意 有 \(n\) 个圆形的桌子排成一排,每个桌子有 \(m\) 个座位. 最开始每个位置上都有一个人.现在每个人都要重新选择一个座位,第 \(i\) 桌的第 \(j\) 个人的新座位只能在第 ...

  10. 超详细的Guava RateLimiter限流原理解析

    超详细的Guava RateLimiter限流原理解析  mp.weixin.qq.com 点击上方“方志朋”,选择“置顶或者星标” 你的关注意义重大! 限流是保护高并发系统的三把利器之一,另外两个是 ...