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中,设置相应的签名。

    测试通过。

  • 相关阅读:
    persistence_timeout ,域名请求登录后一操作即被踢出,,KeepAlive,lvs
    记录因xen而导致lvs,realserver转发activeconn为0
    html5各种页面切换效果和模态对话框
    [设计模式] javascript 之 抽象工厂模式
    jQuery源码分析-构造函数详解
    CSS3动画的回调处理
    jQuery load()方法用法集锦!
    css控制input标签
    分享22款响应式的 jQuery 图片滑块插件
    Jquery取得iframe中元素的几种方法(转载)
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/14429733.html
Copyright © 2011-2022 走看看