zoukankan      html  css  js  c++  java
  • 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);
        }
    }
    

      

  • 相关阅读:
    Log4J日志整合及配置详解
    SmartGit/Hg 4.0.1 发布
    Test Kitchen 0.7.0支持在OpenStack上对Opscode Chef进行集成测试
    RubyGNOME2 1.2.0 发布,支持 GTK+ 3
    PowerDNS Recursor 3.5 RC1 发布
    用于展现图表的50种JavaScript库
    Lambda表达式现状分析
    Node.js 0.8.18 / 0.9.7 发布
    CRUX 3.0 发布,轻量级 Linux 发行版
    Google 更新浏览器内的手写识别技术
  • 原文地址:https://www.cnblogs.com/woolhc/p/6963425.html
Copyright © 2011-2022 走看看