Shiro實戰(六) - 許可權快取
1 概述


Shiro提供了類似於Spring的Cache抽象,即Shiro本身不實現Cache,但是對Cache進行了又抽象,方便更換不同的底層Cache實現。
Shiro提供的Cache介面:
public interface Cache<K, V> { //根據Key獲取快取中的值 public V get(K key) throws CacheException; //往快取中放入key-value,返回快取中之前的值 public V put(K key, V value) throws CacheException; //移除快取中key對應的值,返回該值 public V remove(K key) throws CacheException; //清空整個快取 public void clear() throws CacheException; //返回快取大小 public int size(); //獲取快取中所有的key public Set<K> keys(); //獲取快取中所有的value public Collection<V> values(); }
-
Shiro提供的CacheManager介面
-
Shiro還提供了CacheManagerAware用於注入CacheManager
Shiro內部相應的元件(DefaultSecurityManager)會自動檢測相應的物件(如Realm)是否實現了CacheManagerAware並自動注入相應的CacheManager
2 Realm快取
Shiro提供了CachingRealm,其實現了CacheManagerAware介面,提供了快取的一些基礎實現;另外AuthenticatingRealm及AuthorizingRealm分別提供了對AuthenticationInfo 和AuthorizationInfo資訊的快取
- ini配置
userRealm=com.sss.realm.UserRealm userRealm.credentialsMatcher=$credentialsMatcher userRealm.cachingEnabled=true userRealm.authenticationCachingEnabled=true userRealm.authenticationCacheName=authenticationCache userRealm.authorizationCachingEnabled=true userRealm.authorizationCacheName=authorizationCache securityManager.realms=$userRealm cacheManager=org.apache.shiro.cache.ehcache.EhCacheManager cacheManager.cacheManagerConfigFile=classpath:shiro-ehcache.xml securityManager.cacheManager=$cacheManager;