欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

shiro緩存機(jī)實(shí)例代碼

 更新時(shí)間:2017年09月19日 15:59:23   作者:動(dòng)力節(jié)點(diǎn)  
Shiro提供了類似于Spring的Cache抽象,即Shiro本身不實(shí)現(xiàn)Cache,但是對(duì)Cache進(jìn)行了又抽象,方便更換不同的底層Cache實(shí)現(xiàn)

Shiro提供了類似于Spring的Cache抽象,即Shiro本身不實(shí)現(xiàn)Cache,但是對(duì)Cache進(jìn)行了又抽象,方便更換不同的底層Cache實(shí)現(xiàn)。 

Shiro提供的Cache接口: 

Java代碼  

public interface Cache<K, V> { 
 //根據(jù)Key獲取緩存中的值 
 public V get(K key) throws CacheException; 
 //往緩存中放入key-value,返回緩存中之前的值 
 public V put(K key, V value) throws CacheException; 
 //移除緩存中key對(duì)應(yīng)的值,返回該值 
 public V remove(K key) throws CacheException; 
 //清空整個(gè)緩存 
 public void clear() throws CacheException; 
 //返回緩存大小 
 public int size(); 
 //獲取緩存中所有的key 
 public Set<K> keys(); 
 //獲取緩存中所有的value 
 public Collection<V> values(); 
} 

Shiro提供的CacheManager接口: 

Java代碼 

public interface CacheManager { 
 //根據(jù)緩存名字獲取一個(gè)Cache 
 public <K, V> Cache<K, V> getCache(String name) throws CacheException; 
} 

Shiro還提供了CacheManagerAware用于注入CacheManager: 

Java代碼  

public interface CacheManagerAware { 
 //注入CacheManager 
 void setCacheManager(CacheManager cacheManager); 
} 

Shiro內(nèi)部相應(yīng)的組件(DefaultSecurityManager)會(huì)自動(dòng)檢測(cè)相應(yīng)的對(duì)象(如Realm)是否實(shí)現(xiàn)了CacheManagerAware并自動(dòng)注入相應(yīng)的CacheManager。  

Realm緩存

Shiro提供了CachingRealm,其實(shí)現(xiàn)了CacheManagerAware接口,提供了緩存的一些基礎(chǔ)實(shí)現(xiàn);另外AuthenticatingRealm及AuthorizingRealm分別提供了對(duì)AuthenticationInfo 和AuthorizationInfo信息的緩存。 

ini配置   

Java代碼  

userRealm=com.github.zhangkaitao.shiro.chapter11.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 

userRealm.cachingEnabled:?jiǎn)⒂镁彺?,默認(rèn)false;
userRealm.authenticationCachingEnabled:?jiǎn)⒂蒙矸蒡?yàn)證緩存,即緩存AuthenticationInfo信息,默認(rèn)false;
userRealm.authenticationCacheName:緩存AuthenticationInfo信息的緩存名稱;
userRealm. authorizationCachingEnabled:?jiǎn)⒂檬跈?quán)緩存,即緩存AuthorizationInfo信息,默認(rèn)false;
userRealm. authorizationCacheName:緩存AuthorizationInfo信息的緩存名稱;

cacheManager:緩存管理器,此處使用EhCacheManager,即Ehcache實(shí)現(xiàn),需要導(dǎo)入相應(yīng)的Ehcache依賴,請(qǐng)參考pom.xml; 

因?yàn)闇y(cè)試用例的關(guān)系,需要將Ehcache的CacheManager改為使用VM單例模式:

this.manager = new net.sf.ehcache.CacheManager(getCacheManagerConfigFileInputStream());

改為

this.manager = net.sf.ehcache.CacheManager.create(getCacheManagerConfigFileInputStream()); 

測(cè)試用例 

Java代碼  

@Test 
public void testClearCachedAuthenticationInfo() { 
 login(u1.getUsername(), password); 
 userService.changePassword(u1.getId(), password + "1"); 
 
 RealmSecurityManager securityManager = 
  (RealmSecurityManager) SecurityUtils.getSecurityManager(); 
 UserRealm userRealm = (UserRealm) securityManager.getRealms().iterator().next(); 
 userRealm.clearCachedAuthenticationInfo(subject().getPrincipals()); 
 login(u1.getUsername(), password + "1"); 
} 

首先登錄成功(此時(shí)會(huì)緩存相應(yīng)的AuthenticationInfo),然后修改密碼;此時(shí)密碼就變了;接著需要調(diào)用Realm的clearCachedAuthenticationInfo方法清空之前緩存的AuthenticationInfo;否則下次登錄時(shí)還會(huì)獲取到修改密碼之前的那個(gè)AuthenticationInfo;

Java代碼  

@Test 
public void testClearCachedAuthorizationInfo() { 
 login(u1.getUsername(), password); 
 subject().checkRole(r1.getRole()); 
 userService.correlationRoles(u1.getId(), r2.getId()); 
 
 RealmSecurityManager securityManager = 
  (RealmSecurityManager) SecurityUtils.getSecurityManager(); 
 UserRealm userRealm = (UserRealm)securityManager.getRealms().iterator().next(); 
 userRealm.clearCachedAuthorizationInfo(subject().getPrincipals()); 
 subject().checkRole(r2.getRole()); 
} 

和之前的用例差不多;此處調(diào)用Realm的clearCachedAuthorizationInfo清空之前緩存的AuthorizationInfo; 

另外還有clearCache,其同時(shí)調(diào)用clearCachedAuthenticationInfo和clearCachedAuthorizationInfo,清空AuthenticationInfo和AuthorizationInfo。 

UserRealm還提供了clearAllCachedAuthorizationInfo、clearAllCachedAuthenticationInfo、clearAllCache,用于清空整個(gè)緩存。

在某些清空下這種方式可能不是最好的選擇,可以考慮直接廢棄Shiro的緩存,然后自己通過如AOP機(jī)制實(shí)現(xiàn)自己的緩存;可以參考:

https://github.com/zhangkaitao/es/tree/master/web/src/main/java/com/sishuok/es/extra/aop

另外如果和Spring集成時(shí)可以考慮直接使用Spring的Cache抽象,可以考慮使用SpringCacheManagerWrapper,其對(duì)Spring Cache進(jìn)行了包裝,轉(zhuǎn)換為Shiro的CacheManager實(shí)現(xiàn):

https://github.com/zhangkaitao/es/blob/master/web/src/main/java/org/apache/shiro/cache/spring/SpringCacheManagerWrapper.java  

Session緩存

當(dāng)我們?cè)O(shè)置了SecurityManager的CacheManager時(shí),如:

Java代碼  

securityManager.cacheManager=$cacheManager 

當(dāng)我們?cè)O(shè)置SessionManager時(shí):

Java代碼 

sessionManager=org.apache.shiro.session.mgt.DefaultSessionManager 
securityManager.sessionManager=$sessionManager 

如securityManager實(shí)現(xiàn)了SessionsSecurityManager,其會(huì)自動(dòng)判斷SessionManager是否實(shí)現(xiàn)了CacheManagerAware接口,如果實(shí)現(xiàn)了會(huì)把CacheManager設(shè)置給它。然后sessionManager會(huì)判斷相應(yīng)的sessionDAO(如繼承自CachingSessionDAO)是否實(shí)現(xiàn)了CacheManagerAware,如果實(shí)現(xiàn)了會(huì)把CacheManager設(shè)置給它;如第九章的MySessionDAO就是帶緩存的SessionDAO;其會(huì)先查緩存,如果找不到才查數(shù)據(jù)庫。

對(duì)于CachingSessionDAO,可以通過如下配置設(shè)置緩存的名稱:

Java代碼  

sessionDAO=com.github.zhangkaitao.shiro.chapter11.session.dao.MySessionDAO 
sessionDAO.activeSessionsCacheName=shiro-activeSessionCache 
activeSessionsCacheName默認(rèn)就是shiro-activeSessionCache。

總結(jié)

以上所述是小編給大家介紹的shiro緩存機(jī)實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Linux下設(shè)置每天自動(dòng)備份數(shù)據(jù)庫的方法

    Linux下設(shè)置每天自動(dòng)備份數(shù)據(jù)庫的方法

    這篇文章主要介紹了Linux下設(shè)置每天自動(dòng)備份數(shù)據(jù)庫的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • linux 下隱藏進(jìn)程的一種方法及遇到的坑

    linux 下隱藏進(jìn)程的一種方法及遇到的坑

    這篇文章主要介紹了linux 下隱藏進(jìn)程的一種方法,主要實(shí)現(xiàn)思路就是利用 LD_PRELOAD 來實(shí)現(xiàn)系統(tǒng)函數(shù)的劫持,具體實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2018-09-09
  • Linux字符終端如何用鼠標(biāo)移動(dòng)一個(gè)紅色矩形詳解

    Linux字符終端如何用鼠標(biāo)移動(dòng)一個(gè)紅色矩形詳解

    這篇文章主要給大家介紹了關(guān)于Linux字符終端如何用鼠標(biāo)移動(dòng)一個(gè)紅色矩形的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Linux具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • 使用 Apache Superset 可視化 ClickHouse 數(shù)據(jù)的兩種方法

    使用 Apache Superset 可視化 ClickHouse 數(shù)據(jù)的兩種方法

    Apache Superset是一個(gè)強(qiáng)大的BI工具,它提供了查看和探索數(shù)據(jù)的方法。它在 ClickHouse 用戶中也越來越受歡迎。今天將介紹安裝 Superset 的 2 種方法,通過示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-06-06
  • Flume環(huán)境部署和配置詳解及案例大全

    Flume環(huán)境部署和配置詳解及案例大全

    flume是一個(gè)分布式、可靠、和高可用的海量日志采集、聚合和傳輸?shù)南到y(tǒng)。支持在日志系統(tǒng)中定制各類數(shù)據(jù)發(fā)送方,用于收集數(shù)據(jù);同時(shí),F(xiàn)lume提供對(duì)數(shù)據(jù)進(jìn)行簡(jiǎn)單處理,并寫到各種數(shù)據(jù)接受方(比如文本、HDFS、Hbase等)的能力 。
    2014-08-08
  • Linux下指定源ip進(jìn)行ping操作的方法

    Linux下指定源ip進(jìn)行ping操作的方法

    今天小編就為大家分享一篇Linux下指定源ip進(jìn)行ping操作的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Linux系統(tǒng)下使用U盤的方法

    Linux系統(tǒng)下使用U盤的方法

    在linux系統(tǒng)之中, 一切設(shè)備皆文件, 所以我們的U盤也是一個(gè)文件.磁盤設(shè)備被抽象成sda文件, U盤設(shè)備被抽象成sdb文件。這篇文章主要介紹了Linux系統(tǒng)下使用U盤的方法,需要的朋友可以參考下
    2016-10-10
  • linux查看防火墻狀態(tài)與開啟關(guān)閉命令詳解

    linux查看防火墻狀態(tài)與開啟關(guān)閉命令詳解

    linux查看防火墻狀態(tài)與開啟關(guān)閉命令常用的有以下兩種方式,大家可以參考一下
    2018-03-03
  • linux系統(tǒng)報(bào)xfs_vm_releasepage警告問題的處理方法

    linux系統(tǒng)報(bào)xfs_vm_releasepage警告問題的處理方法

    這篇文章主要給大家介紹了關(guān)于linux系統(tǒng)報(bào)xfs_vm_releasepage警告問題的處理方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用linux系統(tǒng)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Linux Crontab Shell腳本實(shí)現(xiàn)秒級(jí)定時(shí)任務(wù)的方法

    Linux Crontab Shell腳本實(shí)現(xiàn)秒級(jí)定時(shí)任務(wù)的方法

    這篇文章主要介紹了Linux Crontab Shell腳本實(shí)現(xiàn)秒級(jí)定時(shí)任務(wù)的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11

最新評(píng)論