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);
    }
}

  

posted @ 2017-06-08 16:15  huangcqen  阅读(2181)  评论(0编辑  收藏  举报