Shiro SessionDAO的设计概念
SessionDao
其行为有
AbstractSessionDAO
具备了生成Session的ID的工具SessionIdGenerator,该工具有多种功能(默认是UUID形式的),可以生成UUID形式的,可以是随机数形式的
公共的方法抽取出来交给AbstractSessionDao来做,如生成Session的ID,如新增、查询、更新、删除Session过程中涉及的行为(四种基础行为抽象)
public Serializable create(Session session) { Serializable sessionId = doCreate(session); verifySessionId(sessionId); return sessionId; } private void verifySessionId(Serializable sessionId) { if (sessionId == null) { String msg = "sessionId returned from doCreate implementation is null. Please verify the implementation."; throw new IllegalStateException(msg); } } protected abstract Serializable doCreate(Session session);
MemorySessionDAO具体实现新增行为
protected Serializable doCreate(Session session) { Serializable sessionId = generateSessionId(session); assignSessionId(session, sessionId); storeSession(sessionId, session); return sessionId; }
将Session存储到了内存中
private ConcurrentMap<Serializable, Session> sessions; public MemorySessionDAO() { this.sessions = new ConcurrentHashMap<Serializable, Session>(); } protected Session storeSession(Serializable id, Session session) { if (id == null) { throw new NullPointerException("id argument cannot be null."); } return sessions.putIfAbsent(id, session); }