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

最新文章

  1. WebAPI接收JSON参数注意事项
  2. Android五岁了
  3. C#怎样保证弹出窗体是唯一并居中显示
  4. JS json的使用
  5. 创建一个叫做People的类: 属性:姓名、年龄、性别、身高 行为:说话、计算加法、改名 编写能为所有属性赋值的构造方法; (2)创建主类: 创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80; 让该对象调用成员方法: 说出“你好!” 计算23+45的值 将名字改为“李四”
  6. Java为什么能跨平台运行
  7. centos挂载移动硬盘
  8. django随笔说明
  9. CSS三角形制作样式
  10. top使用命令
  11. Performing User-Managed Database-18.4、Restoring Datafiles and Archived Redo Logs
  12. SSL交互过程
  13. oracle02
  14. YAML基础教程
  15. 如何在centos6.5中安装MySQL数据库
  16. IO复用,AIO,BIO,NIO,同步,异步,阻塞和非阻塞 区别(百度)
  17. 函数datetime
  18. Ajax初窥
  19. Python3 下安装python-votesmart
  20. C#/Sqlite-SQLite PetaPoco django 打造桌面程序

热门文章

  1. linux环境下在springboot项目中获取项目路径(用于保存文件等)
  2. java篇 之 流程控制语句
  3. 【NLP】BLEU值满分是100分吗?
  4. bs4模块
  5. JSON.stringify的三个参数(转载)
  6. promise和setTimeout执行顺序的问题
  7. mysql8.0.15二进制安装
  8. sshpass-Linux命令之非交互SSH密码验证
  9. 20165232 week1 kali安装
  10. mp的猜猜看