shiro的session都是存在缓存中的,所有会有一个sessionDAO的类来做CRUD操作,这个类就是org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO。
它继承了CachingSessionDAO这个类,而这个类又是AbstractSessionDAO的子类和CacheManagerAware的实现类,源码如下:
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.
}
大家也许会好奇为什么doReadSession、doUpdate和doDelete方法没有实体代码,我们先来看看最基础类SessionDAO类,它是一个接口其中有create、readSession、update、delete、getActiveSessions这五个方法,而AbstractSessionDAO是在这个接口之上加入了自己的部分代码,并且实现了create和readSession这两个方法:
public Serializable create(Session session) {
Serializable sessionId = doCreate(session);
verifySessionId(sessionId);
return sessionId;
}
public Session readSession(Serializable sessionId) throws UnknownSessionException {
Session s = doReadSession(sessionId);
if (s == null) {
throw new UnknownSessionException("There is no session with id [" + sessionId + "]");
}
return s;
}
从上面的方法可以看出,两个方法依赖于doCreate和doReadSession这两个方法,而这两个方法又是AbstractSessionDAO的抽象类,所以正常来说子类CachingSessionDAO类应该实现doCreate和doReadSession这两个方法以及其它接口方法update、delete、getActiveSessions但真的是这样吗?我们来看一段源码:
package org.apache.shiro.session.mgt.eis;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.CacheManagerAware;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.ValidatingSession;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
public abstract class CachingSessionDAO extends AbstractSessionDAO implements CacheManagerAware {
......
public Serializable create(Session session) {
Serializable sessionId = super.create(session);
cache(session, sessionId);
return sessionId;
}
.....
public Session readSession(Serializable sessionId) throws UnknownSessionException {
Session s = getCachedSession(sessionId);
if (s == null) {
s = super.readSession(sessionId);
}
return s;
}
....
public void update(Session session) throws UnknownSessionException {
doUpdate(session);
if (session instanceof ValidatingSession) {
if (((ValidatingSession) session).isValid()) {
cache(session, session.getId());
} else {
uncache(session);
}
} else {
cache(session, session.getId());
}
}
.....
protected abstract void doUpdate(Session session);
.....
public void delete(Session session) {
uncache(session);
doDelete(session);
}
....
protected abstract void doDelete(Session session);
....
public Collection<Session> getActiveSessions() {
Cache<Serializable, Session> cache = getActiveSessionsCacheLazy();
if (cache != null) {
return cache.values();
} else {
return Collections.emptySet();
}
}
}
源码的具体内容太长了在这不都展示了只看其中关键的,从上面的代码可以看出,CachingSessionDAO 这个类不仅实现AbstractSessionDAO未实现的接口,而且还重写了已经实现的接口并且在代码中加入了cache的处理。
在这个类中有两个方法已经重新声明就是doUpdate和doDelete,但还有两个方法是一直没有实现的就是doCreate和doReadSession这两个方法,从create方法可以看出它调用的super的create方法,而super类也就是AbstractSessionDAO类的create方法依赖于doCreate方法,所以当类继承到EnterpriseCacheSessionDAO这一层时它只需要去实现doCreate方法就行了其实方法已经没有必要去实现了。
在这里我还要说一点就是EnterpriseCacheSessionDAO这个类的构造方法。这个构造方法是给父类的cacheManager管理器赋值,但好像仅仅是为了保证整个shiro可以正常运行而做的,因为正常加载完之后这个cacheManager就会被重新赋值为你在xml里面配的那,如下代码:
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="shiroDbRealm" />
<property name="sessionManager" ref="sessionManager" />
<!-- 缓存管理器 -->
<property name="cacheManager" ref="shiroCacheManager" />
</bean>
这其中的具体原因不得而知,经过debug也没找到具体入口在哪,所以有知道的朋友请留言分享一下,3Q了