spring mvc+Mybatis整合shiro 第四章 SessionDAO
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了