第一步:在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次將會 出現錯誤次數過多異常
測試結果是沒問題的,不在黏貼