zoukankan      html  css  js  c++  java
  • shiro中用redis做session缓存

    shiro中的cache和spring类似,有提供两个接口,使用者使用不同的实现来继承他们:

      1.cache-实际进行缓存操作,如使用spring-data-redis操作

      2.cacheManager-管理cahe实例,返回cache实例

      3.SessionDAO-调用cache进行操作

    项目中,我用的securityManager是DefaultWebSecurityManager

    public class DefaultWebSecurityManager extends DefaultSecurityManager implements WebSecurityManager 

    该类实现了DefaultSecurityManager

    public class DefaultSecurityManager extends SessionsSecurityManager 

    其中SessionsSecurityManager是CachingSecurityManager的子类,而CachingSecurityManager类实现了CacheManagerAware接口,该接口会注入我们在xml配置好的cacheManager;

    public interface CacheManagerAware {
    
        /**
         * Sets the available CacheManager instance on this component.
         *
         * @param cacheManager the CacheManager instance to set on this component.
         */
        void setCacheManager(CacheManager cacheManager);
    }

    具体的session缓存过程,是利用sessionDao来完成的,我用的是EnterpriseCacheSessionDAO

    public class EnterpriseCacheSessionDAO extends CachingSessionDAO {
    
        public EnterpriseCacheSessionDAO() {
            setCacheManager(new AbstractCacheManager() {
                @Override
                protected Cache<Serializable, Session> createCache(String name) throws CacheException {
                    return new MapCache<Serializable, Session>(name, new ConcurrentHashMap<Serializable, Session>());
                }
            });
        }
    
        protected Serializable doCreate(Session session) {
            Serializable sessionId = generateSessionId(session);
            assignSessionId(session, sessionId);
            return sessionId;
        }
    
        protected Session doReadSession(Serializable sessionId) {
            return null; //should never execute because this implementation relies on parent class to access cache, which
            //is where all sessions reside - it is the cache implementation that determines if the
            //cache is memory only or disk-persistent, etc.
        }
    
        protected void doUpdate(Session session) {
            //does nothing - parent class persists to cache.
        }
    
        protected void doDelete(Session session) {
            //does nothing - parent class removes from cache.
        }
    }

    这里面有基本的crud方法;

    至于shito是如何调用这些类以及方法的,还在摸索中……未完待续

  • 相关阅读:
    dos
    jsf session的获取和添加
    tomcat多开造成的端口占用
    myeclipse中tomcat7内存大小的设置
    中文传值乱码过滤
    java定时器
    jsf中jstl标签<c:forEach/>打印信息
    python基础python函数any()与all()的区别
    C# GetManifestResourceStream获取资源为null
    Linux kill 9 和 kill 15 的区别
  • 原文地址:https://www.cnblogs.com/jkavor/p/7264007.html
Copyright © 2011-2022 走看看