很久沒有寫過博文,,一來是因為工作比較緊,,二來是因為很長一段時間沒有寫博文的心情,。今天,,想繼續(xù)寫寫shiro的一些文章,,這篇文章只要想分享的是shiro 共享session的內(nèi)容,。
在這里先給出我的shiro配置文件,。
- <bean id="shiroRealm" class="com.concom.security.infrastructure.shiro.ShiroRealm"/>
-
- <bean id="shiroCacheManager" class="com.concom.security.infrastructure.shiro.cache.ShiroCacheManager"/>
-
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <property name="realm" ref="shiroRealm" />
- <!-- <property name="sessionMode" value="native"/> -->
- <property name="cacheManager" ref="shiroCacheManager"/>
- <property name="sessionManager" ref="shiroSessionManager"/>
- <!-- <property name="subjectDAO" ref="subjectDAO"/> -->
- </bean>
-
-
- <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
- <!-- <property name="sessionIdGenerator" ref="sessionIdGenerator"/> -->
- </bean>
- <!-- <bean id="sessionIdGenerator" class="com.concom.security.infrastructure.shiro.ShiroSessionIdGenerator"/> -->
-
- <bean id="shiroSessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
- <property name="sessionDAO" ref="sessionDAO"/>
- <!-- <property name="sessionValidationScheduler" ref="shiroSessionValidationScheduler"/> -->
- <property name="sessionValidationInterval" value="1800000"/> <!-- 相隔多久檢查一次session的有效性 -->
- <property name="globalSessionTimeout" value="1800000"/> <!-- session 有效時間為半小時 (毫秒單位)-->
- <property name="sessionIdCookie.domain" value="${domain}"/>
- <property name="sessionIdCookie.name" value="${sessionId_name}"/>
- <property name="sessionIdCookie.path" value="${path}"/>
- <!-- <property name="sessionListeners">
- <list>
- <bean class="com.concom.security.interfaces.listener.SessionListener"/>
- </list>
- </property> -->
- </bean>
-
-
- <!-- <bean id="shiroSessionValidationScheduler" class="org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler">
- <constructor-arg name="sessionManager" ref="sessionManager"/>
- <property name="interval" value="10000"/> 定時半小時檢驗一次有所session是否有效(毫秒單位)
- </bean> -->
-
-
- <!-- Post processor that automatically invokes init() and destroy() methods -->
- <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
從shiro的DefaultWebSecurityManager源碼中我們可以看到,,該類繼承了CachingSecurityManager類,CachingSecurityManager里有個CacheManager屬性,,這就是shiro的cache,,我們主要實現(xiàn)這個cache就可以。貼出源碼,。
- public abstract class CachingSecurityManager implements SecurityManager, Destroyable, CacheManagerAware {
-
- /**
- * The CacheManager to use to perform caching operations to enhance performance. Can be null.
- */
- private CacheManager cacheManager;
-
- /**
- * Default no-arg constructor that will automatically attempt to initialize a default cacheManager
- */
- public CachingSecurityManager() {
- }
-
- /**
- * Returns the CacheManager used by this SecurityManager.
- *
- * @return the cacheManager used by this SecurityManager
- */
- public CacheManager getCacheManager() {
- return cacheManager;
- }
-
- /**
- * Sets the CacheManager used by this {@code SecurityManager} and potentially any of its
- * children components.
- * <p/>
- * After the cacheManager attribute has been set, the template method
- * {@link #afterCacheManagerSet afterCacheManagerSet()} is executed to allow subclasses to adjust when a
- * cacheManager is available.
- *
- * @param cacheManager the CacheManager used by this {@code SecurityManager} and potentially any of its
- * children components.
- */
- public void setCacheManager(CacheManager cacheManager) {
- this.cacheManager = cacheManager;
- afterCacheManagerSet();
- }
-
- /**
- * Template callback to notify subclasses that a
- * {@link org.apache.shiro.cache.CacheManager CacheManager} has been set and is available for use via the
- * {@link #getCacheManager getCacheManager()} method.
- */
- protected void afterCacheManagerSet() {
- }
-
- /**
- * Destroys the {@link #getCacheManager() cacheManager} via {@link LifecycleUtils#destroy LifecycleUtils.destroy}.
- */
- public void destroy() {
- LifecycleUtils.destroy(getCacheManager());
- this.cacheManager = null;
- }
-
- }
cacheManager是一個接口,,shiro給出了一個實現(xiàn),就是MemoryConstrainedCacheManager,,下面也貼出MemoryConstrainedCacheManager的源碼,。
- public class MemoryConstrainedCacheManager extends AbstractCacheManager {
-
- /**
- * Returns a new {@link MapCache MapCache} instance backed by a {@link SoftHashMap}.
- *
- * @param name the name of the cache
- * @return a new {@link MapCache MapCache} instance backed by a {@link SoftHashMap}.
- */
- @Override
- protected Cache createCache(String name) {
- return new MapCache<Object, Object>(name, new SoftHashMap<Object, Object>());
- }
- }
從MemoryConstrainedCacheManager我們可以看到,MemoryConstrainedCacheManager繼承了AbstractCacheManager,,我們自己建一個類,,也繼承AbstractCacheManager,然后實現(xiàn)createCache(String name)方法,,替換shiro的默認實現(xiàn)即可,。在這里,,也給出我的實現(xiàn)。
- package com.concom.security.infrastructure.shiro.cache;
-
- import org.apache.shiro.cache.AbstractCacheManager;
- import org.apache.shiro.cache.Cache;
- import org.apache.shiro.cache.CacheException;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import com.concom.security.infrastructure.shiro.cache.memcache.ShiroMemcachedCache;
-
- /**
- * @author Dylan
- * @time 2014年1月6日
- */
- @SuppressWarnings("rawtypes")
- public class ShiroCacheManager extends AbstractCacheManager {
-
- private static final Logger log = LoggerFactory.getLogger(ShiroCacheManager.class);
-
- @Override
- protected Cache createCache(String cacheName) throws CacheException {
-
- if (log.isTraceEnabled()) {
- log.trace("create a cache name : " + cacheName);
- }
-
- return new ShiroMemcachedCache<Object, Object>(cacheName);
- }
-
- }
createCache方法返回的是一個Cache,,Cache也是一個接口,,我寫了一個ShiroMemcachedCache,實現(xiàn)了Cache,。ShiroMemcachedCache主要是對緩存的一些增刪改查的操作,。其中ShiroMemcachedCache的實現(xiàn)如下:
上面緩存用的是memcached。這樣,,shiro的緩存共享就完成了,。篇幅有點長,這篇先寫到這里,,下一篇將繼續(xù)講shiro的cache共享,。
|