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是如何调用这些类以及方法的,还在摸索中……未完待续

原文地址:https://www.cnblogs.com/jkavor/p/7264007.html