EHCACHE實現登錄錯誤次數賬號鎖定


使用EHCACHE實現賬號密碼登錄校驗失敗5次鎖定10分鍾

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <diskStore path="java.io.tmpdir"/>

    <defaultCache
            maxElementsInMemory="10000"   
            eternal="false"
            overflowToDisk="false"
            timeToIdleSeconds="10"
            timeToLiveSeconds="20"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"/>

    <cache name="cache"
           maxEntriesLocalHeap="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>
    
    <cache name="limitRetryCache"
           maxEntriesLocalHeap="5000"  
           eternal="false"
           timeToIdleSeconds="600"    <!--緩存創建后未被訪問銷毀時間-->
           timeToLiveSeconds="3600"   <!--緩存從創建到銷毀時間-->
           overflowToDisk="false"
           statistics="false">
    </cache>
    
</ehcache>

 Spring 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!--ehcache-->
    <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
    </bean>


    <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManager" ref="ehcacheManager"/>
    </bean>


    <bean id="retryLimitMatcher" class="com.haier.basic.cache.RetryLimitMatcher">
        <constructor-arg ref="shiroCacheManager"/>
    </bean>

</beans>
package com.haier.basic.cache;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by liujianji on 2017/6/1.
 */
public class RetryLimitMatcher{
    private Cache<String, AtomicInteger> passwordRetryCache;

    public RetryLimitMatcher(CacheManager cacheManager) {
        passwordRetryCache = cacheManager.getCache("limitRetryCache");
    }

    public void doLimitMatch(String userTel) {
        AtomicInteger retryCount = passwordRetryCache.get(userTel);
        if(retryCount == null) {
            retryCount = new AtomicInteger(0);
            passwordRetryCache.put(userTel, retryCount);
        }
        retryCount.incrementAndGet();
        passwordRetryCache.put(userTel,retryCount);
    }

    public boolean isLocked(String userTel){
        AtomicInteger num = passwordRetryCache.get(userTel);
        if(null != num && num.intValue() > 4){
            return true;
        }
        return false;
    }

    public int getRetaryNum(String userTel){
        AtomicInteger num = passwordRetryCache.get(userTel);
        if(null == num){
            return 5;
        }
        return 5 - num.intValue();
    }


    public void removeRetary(String userTel){
        passwordRetryCache.remove(userTel);
    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM