接著上一篇部落格來講:Spring Boot整合jpa,Shiro進行許可權管理
Shiro預設整合了EhCache,來實現緩衝,如果我們想用redis替換EhCache來實現緩衝怎麼做了。我們可以從Shiro的源碼來找到一些端倪。我們可以類比EhCacheManager的實現方式,EhCacheManager類定義如下:
public class EhCacheManager implements CacheManager, Initializable, Destroyable {}
我們從上面的代碼可以看到,最終要的是實現了CacheManager介面,該介面很簡單,只有一個方法:
public interface CacheManager { /** * Acquires the cache with the specified <code>name</code>. If a cache does not yet exist with that name, a new one * will be created with that name and returned. * * @param name the name of the cache to acquire. * @return the Cache with the given name * @throws CacheException if there is an error acquiring the Cache instance. */ public <K, V> Cache<K, V> getCache(String name) throws CacheException;}
從上面的注釋中,我們可以發現,這個介面需要一個Cache,通過name來擷取Cache,首先,我們來實現CacheManager這個介面
@Servicepublic class RedisCacheManager implements CacheManager {@Autowiredprivate RedisTemplate redisTemplate; // RedisTemplate,如果不明白怎麼使用的,請參考http://blog.csdn.net/liuchuanhong1/article/details/54601037@Overridepublic <K, V> Cache<K, V> getCache(String name) throws CacheException {System.out.println("name:"+name);return new RedisCache<K, V>(120, redisTemplate);// 為了簡化代碼的編寫,此處直接new一個Cache}}
下面,我們來看下這個Cache怎麼寫
package com.chhliu.springboot.shiro.cache;import java.util.Collection;import java.util.Set;import java.util.concurrent.TimeUnit;import org.apache.shiro.cache.Cache;import org.apache.shiro.cache.CacheException;import org.springframework.data.redis.core.RedisTemplate;public class RedisCache<K, V> implements Cache<K, V> {private long expireTime = 120;// 緩衝的逾時時間,單位為sprivate RedisTemplate<K, V> redisTemplate;// 通過構造方法注入該對象public RedisCache() {super();}public RedisCache(long expireTime, RedisTemplate<K, V> redisTemplate) {super();this.expireTime = expireTime;this.redisTemplate = redisTemplate;}/** * 通過key來擷取對應的緩衝對象 * 通過源碼我們可以發現,shiro需要的key的類型為Object,V的類型為AuthorizationInfo對象 */@Overridepublic V get(K key) throws CacheException {return redisTemplate.opsForValue().get(key);}/** * 將許可權資訊加入緩衝中 */@Overridepublic V put(K key, V value) throws CacheException {redisTemplate.opsForValue().set(key, value, this.expireTime, TimeUnit.SECONDS);return value;}/** * 將許可權資訊從緩衝中刪除 */@Overridepublic V remove(K key) throws CacheException {V v = redisTemplate.opsForValue().get(key);redisTemplate.opsForValue().getOperations().delete(key);return v;}@Overridepublic void clear() throws CacheException {}@Overridepublic int size() {return 0;}@Overridepublic Set<K> keys() {return null;}@Overridepublic Collection<V> values() {return null;}}
這兩步完成之後,就是需要將原來的EhCacheManager的配置換成RedisCacheManager了
@Beanpublic DefaultWebSessionManager configWebSessionManager(){DefaultWebSessionManager manager = new DefaultWebSessionManager();manager.setCacheManager(cacheManager);// 換成Redis的緩衝管理器manager.setSessionDAO(sessionDao);manager.setDeleteInvalidSessions(true);manager.setGlobalSessionTimeout(sessionDao.getExpireTime());manager.setSessionValidationSchedulerEnabled(true);return manager;}
通過上面的幾步,我們就實現了用Redis來緩衝Shiro的許可權等相關資訊