zoukankan      html  css  js  c++  java
  • 用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中,设置相应的签名。

    测试通过。

  • 相关阅读:
    黑产江湖
    FW/IDS/IPS/WAF等安全设备部署方式及优缺点
    SOAPA来临,SIEM时代终结?
    美国爱因斯坦计划4
    零基础如何学好安卓开发
    协同办公系统能为企业带来怎样的影响?
    阿里腾讯开撕,钉钉的广告打到腾讯的地盘了
    bug管理工具为开发者工作带来哪些改变?
    开发人员必备的几款bug管理工具
    教你玩转产品管理系统iClap(PC端功能篇)
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/14429733.html
Copyright © 2011-2022 走看看