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

      

  • 相关阅读:
    WDF驱动中KMDF与UMDF区别
    GTD190018:【翻译】The Case Against Civilization
    GTD190017:【翻译】Transformer: A Novel Neural Network Architecture for Language Understanding
    GTD180016:【翻译】Software Engineering ≠ Computer Science
    GTD180015:【学习】一文看尽深度学习RNN:为啥就它适合语音识别、NLP与机器翻译?
    GTD180014:【翻译】Combinatory_logic
    GTD180013:【翻译】Improving the Realism of Synthetic Images
    GTD180012:【翻译】Visualizing Algorithms
    GTD180011:【翻译】The future of deep learning
    GitHub180001:【翻译】Using 3D Convolutional Neural Networks for Speaker Verification
  • 原文地址:https://www.cnblogs.com/woolhc/p/6963425.html
Copyright © 2011-2022 走看看