• 用redis作为shiro的登陆密码次数记录


    上篇中,因为ehcache的单例原因,这里提供了另外一种方法。

    用redis作为 shiro的密码凭证器的记载体。

    package cn.taotao.shiro.service;
    
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Set;
    import java.util.concurrent.Callable;
    import java.util.concurrent.atomic.AtomicInteger;
    
    import javax.inject.Singleton;
    
    import org.apache.shiro.authc.AuthenticationInfo;
    import org.apache.shiro.authc.AuthenticationToken;
    import org.apache.shiro.authc.ExcessiveAttemptsException;
    import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
    import org.apache.shiro.cache.Cache;
    import org.apache.shiro.cache.CacheManager;
    import org.apache.shiro.cache.ehcache.EhCacheManager;
    import org.apache.shiro.io.ResourceUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cache.Cache.ValueWrapper;
    import org.springframework.context.annotation.Bean;
    import org.springframework.data.redis.cache.RedisCache;
    import org.springframework.stereotype.Service;
    
    import com.hazelcast.internal.serialization.SerializableByConvention;
    
    import redis.clients.jedis.Jedis;
    
    @Service
    public class MyHashedCredentialsMatcher extends HashedCredentialsMatcher {
    
        private Integer retryCount = 0;
        @Autowired
        private Jedis jedis;
    
        public MyHashedCredentialsMatcher(Jedis jedis) {
    
        }
    
    
    
        @Override
        public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
            System.out.println("docredentialsmatch......");
            String username = (String) token.getPrincipal();
            System.out.println("username is issssss" + username);
    
            if (jedis.get(username) == null) {
                jedis.set(username, "0");
            }
            retryCount = Integer.parseInt(jedis.get(username)) + 1;
            System.out.println("retryCount is : =============" + retryCount);
            jedis.set(username,retryCount.toString());
            jedis.expire(username, 600);
            if (retryCount > 5) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
                Date date = new Date(System.currentTimeMillis());
                System.out.println("登录时间 " + formatter.format(date));
                // if retry count > 5 throw
                jedis.expire(username, 2000);
                System.out.println("username: " + username + " tried to login more than 5 times in period");
                throw new ExcessiveAttemptsException(
                        "username: " + username + " tried to login more than 5 times in period");
    
            }
    
            boolean matches = super.doCredentialsMatch(token, info);
            if (matches) {
                // clear retry count
                jedis.del(username);
            }
            return matches;
        }
    
    }

    然后在shiro的config中,设置相应的签名。

    测试通过。

  • 相关阅读:
    结合项目实例 回顾传统设计模式(五)单例模式
    CUDA并行计算框架(二)实例相关。
    结合项目实例 回顾传统设计模式(八)模板方法模式
    结合项目实例 回顾传统设计模式(三)装饰者模式
    结合项目实例 回顾传统设计模式(十一)代理模式
    趣谈.net大型电子商务 亲~ 走过路过不要错过~
    结合项目实例 回顾传统设计模式(九)迭代器模式
    DataTable的操作类
    xslt调用自定义函数(C#/Js/Java)
    优秀的前段框架Bootstrap推荐
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/14429733.html
Copyright © 2020-2023  润新知