apache shiro集群实现(二)— cache共享

上一篇已经解决了第一个问题,session的共享,现在我们解决第二个问题cache的共享。

    先看下spring的配置文件,上一篇已经提到过了

  1. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" depends-on="userRepository,roleRepository">  
  2.     <property name="sessionManager" ref="defaultWebSessionManager" />  
  3.     <property name="realm" ref="shiroDbRealm" />  
  4.     <property name="cacheManager" ref="memoryConstrainedCacheManager" />  
  5. </bean>  
  6. <bean id="memoryConstrainedCacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />  
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" depends-on="userRepository,roleRepository">
	<property name="sessionManager" ref="defaultWebSessionManager" />
	<property name="realm" ref="shiroDbRealm" />
	<property name="cacheManager" ref="memoryConstrainedCacheManager" />
</bean>
<bean id="memoryConstrainedCacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />

    这里cacheManager我们注入了shiro自定的本机内存实现的cacheManager类,当然,这肯定不满足我们集群的需要,所以我们要自己实现cacheManager类,这里我还是用了redis作为cache的存储,先创建CustomShiroCacheManager实现类

  1. public class CustomShiroCacheManager implements CacheManager, Destroyable {  
  2.   
  3.     private ShiroCacheManager shiroCacheManager;  
  4.   
  5.     public ShiroCacheManager getShiroCacheManager() {  
  6.         return shiroCacheManager;  
  7.     }  
  8.   
  9.     public void setShiroCacheManager(ShiroCacheManager shiroCacheManager) {  
  10.         this.shiroCacheManager = shiroCacheManager;  
  11.     }  
  12.   
  13.     @Override  
  14.     public <K, V> Cache<K, V> getCache(String name) throws CacheException {  
  15.         return getShiroCacheManager().getCache(name);  
  16.     }  
  17.   
  18.     @Override  
  19.     public void destroy() throws Exception {  
  20.         shiroCacheManager.destroy();  
  21.     }  
  22.   
  23. }  
public class CustomShiroCacheManager implements CacheManager, Destroyable {

	private ShiroCacheManager shiroCacheManager;

	public ShiroCacheManager getShiroCacheManager() {
		return shiroCacheManager;
	}

	public void setShiroCacheManager(ShiroCacheManager shiroCacheManager) {
		this.shiroCacheManager = shiroCacheManager;
	}

	@Override
	public <K, V> Cache<K, V> getCache(String name) throws CacheException {
		return getShiroCacheManager().getCache(name);
	}

	@Override
	public void destroy() throws Exception {
		shiroCacheManager.destroy();
	}

}

这里为了扩展,引入了ShiroCacheManager接口

  1. public interface ShiroCacheManager {  
  2.   
  3.     <K, V> Cache<K, V> getCache(String name);  
  4.   
  5.     void destroy();  
  6.   
  7. }  
public interface ShiroCacheManager {

	<K, V> Cache<K, V> getCache(String name);

	void destroy();

}
下面我们自己实现redis的cacheManger
  1. public class JedisShiroCacheManager implements ShiroCacheManager {  
  2.   
  3.     @Autowired  
  4.     private JedisCacheManager jedisCacheManager;  
  5.   
  6.     @Override  
  7.     public <K, V> Cache<K, V> getCache(String name) {  
  8.         return new JedisShiroCache<K, V>(name, jedisCacheManager);  
  9.     }  
  10.   
  11.     @Override  
  12.     public void destroy() {  
  13.         jedisCacheManager.getJedis().shutdown();  
  14.     }  
  15.   
  16. }  
public class JedisShiroCacheManager implements ShiroCacheManager {

	@Autowired
	private JedisCacheManager jedisCacheManager;

	@Override
	public <K, V> Cache<K, V> getCache(String name) {
		return new JedisShiroCache<K, V>(name, jedisCacheManager);
	}

	@Override
	public void destroy() {
		jedisCacheManager.getJedis().shutdown();
	}

}

当然,这里仅仅是getCache,我第一次看源码时,也有这样的疑问,cache的add、remove等等方法在哪里实现呢?我们继续看看shiro的源码就会知道答案了

这个是自己relm的AuthorizingRealm中的方法

  1. protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {  
  2.   
  3.         if (principals == null) {  
  4.             return null;  
  5.         }  
  6.   
  7.         AuthorizationInfo info = null;  
  8.   
  9.         if (log.isTraceEnabled()) {  
  10.             log.trace("Retrieving AuthorizationInfo for principals [" + principals + "]");  
  11.         }  
  12.   
  13.         Cache<Object, AuthorizationInfo> cache = getAvailableAuthorizationCache();  
  14.         if (cache != null) {  
  15.             if (log.isTraceEnabled()) {  
  16.                 log.trace("Attempting to retrieve the AuthorizationInfo from cache.");  
  17.             }  
  18.             Object key = getAuthorizationCacheKey(principals);  
  19.             info = cache.get(key);  
  20.             if (log.isTraceEnabled()) {  
  21.                 if (info == null) {  
  22.                     log.trace("No AuthorizationInfo found in cache for principals [" + principals + "]");  
  23.                 } else {  
  24.                     log.trace("AuthorizationInfo found in cache for principals [" + principals + "]");  
  25.                 }  
  26.             }  
  27.         }  
  28.         if (info == null) {  
  29.             // Call template method if the info was not found in a cache   
  30.             info = <STRONG>doGetAuthorizationInfo</STRONG>(principals);  
  31.             // If the info is not null and the cache has been created, then cache the authorization info.   
  32.             if (info != null && cache != null) {  
  33.                 if (log.isTraceEnabled()) {  
  34.                     log.trace("Caching authorization info for principals: [" + principals + "].");  
  35.                 }  
  36.                 Object key = getAuthorizationCacheKey(principals);  
  37.                 <STRONG>cache.put</STRONG>(key, info);  
  38.             }  
  39.         }  
  40.         return info;  
  41.     }  
protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {

        if (principals == null) {
            return null;
        }

        AuthorizationInfo info = null;

        if (log.isTraceEnabled()) {
            log.trace("Retrieving AuthorizationInfo for principals [" + principals + "]");
        }

        Cache<Object, AuthorizationInfo> cache = getAvailableAuthorizationCache();
        if (cache != null) {
            if (log.isTraceEnabled()) {
                log.trace("Attempting to retrieve the AuthorizationInfo from cache.");
            }
            Object key = getAuthorizationCacheKey(principals);
            info = cache.get(key);
            if (log.isTraceEnabled()) {
                if (info == null) {
                    log.trace("No AuthorizationInfo found in cache for principals [" + principals + "]");
                } else {
                    log.trace("AuthorizationInfo found in cache for principals [" + principals + "]");
                }
            }
        }
        if (info == null) {
            // Call template method if the info was not found in a cache
            info = doGetAuthorizationInfo(principals);
            // If the info is not null and the cache has been created, then cache the authorization info.
            if (info != null && cache != null) {
                if (log.isTraceEnabled()) {
                    log.trace("Caching authorization info for principals: [" + principals + "].");
                }
                Object key = getAuthorizationCacheKey(principals);
                cache.put(key, info);
            }
        }
        return info;
    }

如果大家去查查引入就知道我们自定义relm要实现一个叫做doGetAuthorizationInfo()的方法,它的作用是查询授权信息,我们注意加粗的2行,发现其实cache的put方法其实才是cache存储的核心类,其实现都在cache中,所以我们需要实现自己的cache,创建JedisShiroCache类

  1. public class JedisShiroCache<K, V> implements Cache<K, V> {  
  2.   
  3.     private final String REDIS_SHIRO_CACHE = "shiro-cache:";  
  4.   
  5.     private JedisManager jedisManager;  
  6.   
  7.     private String name;  
  8.   
  9.     public JedisShiroCache(String name, JedisManager jedisManager) {  
  10.         this.name = name;  
  11.         this.jedisManager = jedisManager;  
  12.     }  
  13.       
  14.     /** 
  15.      * 自定义relm中的授权/认证的类名加上授权/认证英文名字 
  16.      * @return 
  17.      */  
  18.     public String getName() {  
  19.         if (name == null)  
  20.             return "";  
  21.         return name;  
  22.     }  
  23.   
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.   
  28.     @Override  
  29.     public V get(K key) throws CacheException {  
  30.         byte[] byteKey = SerializeUtil.serialize(getCacheKey(key));  
  31.         byte[] byteValue = jedisManager.getValueByKey(byteKey);  
  32.         return (V) SerializeUtil.deserialize(byteValue);  
  33.     }  
  34.   
  35.     @Override  
  36.     public V put(K key, V value) throws CacheException {  
  37.         V previos = get(key);  
  38.         jedisManager.saveValueByKey(SerializeUtil.serialize(getCacheKey(key)),  
  39.                 SerializeUtil.serialize(value));  
  40.         return previos;  
  41.     }  
  42.   
  43.     @Override  
  44.     public V remove(K key) throws CacheException {  
  45.         V previos = get(key);  
  46.         jedisManager.deleteByKey(SerializeUtil.serialize(getCacheKey(key)));  
  47.         return previos;  
  48.     }  
  49.   
  50.     @Override  
  51.     public void clear() throws CacheException {  
  52.         byte[] keysPattern = SerializeUtil.serialize(this.REDIS_SHIRO_CACHE  
  53.                 + "*");  
  54.         jedisManager.deleteByKeysPattern(keysPattern);  
  55.     }  
  56.   
  57.     @Override  
  58.     public int size() {  
  59.         if (keys() == null)  
  60.             return 0;  
  61.         return keys().size();  
  62.     }  
  63.   
  64.     @Override  
  65.     public Set<K> keys() {  
  66.         Set<byte[]> byteSet = jedisManager.getKeysByKeysPattern(SerializeUtil  
  67.                 .serialize(this.REDIS_SHIRO_CACHE + "*"));  
  68.         Set<K> keys = new HashSet<K>();  
  69.         for (byte[] bs : byteSet) {  
  70.             keys.add((K) SerializeUtil.deserialize(bs));  
  71.         }  
  72.         return keys;  
  73.     }  
  74.   
  75.     @Override  
  76.     public Collection<V> values() {  
  77.         Set<byte[]> byteSet = jedisManager.getKeysByKeysPattern(SerializeUtil  
  78.                 .serialize(this.REDIS_SHIRO_CACHE + "*"));  
  79.         List<V> result = new LinkedList<V>();  
  80.         for (byte[] bs : byteSet) {  
  81.             result.add((V) SerializeUtil.deserialize(jedisManager  
  82.                     .getValueByKey(bs)));  
  83.         }  
  84.         return result;  
  85.     }  
  86.   
  87.     private String getCacheKey(Object key) {  
  88.         return this.REDIS_SHIRO_CACHE + getName() + ":" + key;  
  89.     }  
public class JedisShiroCache<K, V> implements Cache<K, V> {

	private final String REDIS_SHIRO_CACHE = "shiro-cache:";

	private JedisManager jedisManager;

	private String name;

	public JedisShiroCache(String name, JedisManager jedisManager) {
		this.name = name;
		this.jedisManager = jedisManager;
	}
	
	/**
	 * 自定义relm中的授权/认证的类名加上授权/认证英文名字
	 * @return
	 */
	public String getName() {
		if (name == null)
			return "";
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public V get(K key) throws CacheException {
		byte[] byteKey = SerializeUtil.serialize(getCacheKey(key));
		byte[] byteValue = jedisManager.getValueByKey(byteKey);
		return (V) SerializeUtil.deserialize(byteValue);
	}

	@Override
	public V put(K key, V value) throws CacheException {
		V previos = get(key);
		jedisManager.saveValueByKey(SerializeUtil.serialize(getCacheKey(key)),
				SerializeUtil.serialize(value));
		return previos;
	}

	@Override
	public V remove(K key) throws CacheException {
		V previos = get(key);
		jedisManager.deleteByKey(SerializeUtil.serialize(getCacheKey(key)));
		return previos;
	}

	@Override
	public void clear() throws CacheException {
		byte[] keysPattern = SerializeUtil.serialize(this.REDIS_SHIRO_CACHE
				+ "*");
		jedisManager.deleteByKeysPattern(keysPattern);
	}

	@Override
	public int size() {
		if (keys() == null)
			return 0;
		return keys().size();
	}

	@Override
	public Set<K> keys() {
		Set<byte[]> byteSet = jedisManager.getKeysByKeysPattern(SerializeUtil
				.serialize(this.REDIS_SHIRO_CACHE + "*"));
		Set<K> keys = new HashSet<K>();
		for (byte[] bs : byteSet) {
			keys.add((K) SerializeUtil.deserialize(bs));
		}
		return keys;
	}

	@Override
	public Collection<V> values() {
		Set<byte[]> byteSet = jedisManager.getKeysByKeysPattern(SerializeUtil
				.serialize(this.REDIS_SHIRO_CACHE + "*"));
		List<V> result = new LinkedList<V>();
		for (byte[] bs : byteSet) {
			result.add((V) SerializeUtil.deserialize(jedisManager
					.getValueByKey(bs)));
		}
		return result;
	}

	private String getCacheKey(Object key) {
		return this.REDIS_SHIRO_CACHE + getName() + ":" + key;
	}

最后修改spring配置文件

  1. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" depends-on="userRepository,roleRepository">  
  2.     <property name="sessionManager" ref="defaultWebSessionManager" />  
  3.     <property name="realm" ref="shiroDbRealm" />  
  4.     <property name="cacheManager" ref="customShiroCacheManager" />  
  5. </bean>  
  6. <bean id="customShiroCacheManager" class="com.nfschina.fourjoy.security.shiro.custom.cache.CustomShiroCacheManager">  
  7.     <property name="shiroCacheManager" ref="jedisShiroCacheManager" />  
  8. </bean>  
  9.       
  10. <bean id="jedisShiroCacheManager" class="com.nfschina.fourjoy.security.shiro.custom.cache.JedisShiroCacheManager" />  
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" depends-on="userRepository,roleRepository">
 	<property name="sessionManager" ref="defaultWebSessionManager" />
	<property name="realm" ref="shiroDbRealm" />
	<property name="cacheManager" ref="customShiroCacheManager" />
</bean>
<bean id="customShiroCacheManager" class="com.nfschina.fourjoy.security.shiro.custom.cache.CustomShiroCacheManager">
	<property name="shiroCacheManager" ref="jedisShiroCacheManager" />
</bean>
	
<bean id="jedisShiroCacheManager" class="com.nfschina.fourjoy.security.shiro.custom.cache.JedisShiroCacheManager" />

这样就完成了整个shiro集群的配置

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值