shiro整合ehcache
生活随笔
收集整理的這篇文章主要介紹了
shiro整合ehcache
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
shiro的緩存是被shiro的緩存管理器所管理的,即CacheManage,Shiro的用戶認證是沒有提供緩沖機制的,因為每次登陸一次查詢一次數據庫比對一下用戶名密碼,做緩存的必要幾乎是沒有的。
但是shiro的授權將會是大量的數據,shiro的授權緩存是默認開啟的,接下來我們將對shiro的緩沖使用EhCache來管理,之后授權時只有用戶第一次訪問系統的時候會走realm查數據庫,之后就會走緩沖。
注意:用戶正常退出或者非正常退出時都會清空緩沖。
<!-- 緩存管理器 --> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"><property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml" /> </bean><!-- 繼承自AuthorizingRealm的自定義Realm,即指定Shiro驗證用戶登錄的類為自定義的UserRealm.java --> <bean id="adminRealm" class="com.platform.shiro.AdminRealm" /><!-- Shiro默認會使用Servlet容器的Session,可通過sessionMode屬性來指定使用Shiro原生Session --> <!-- 即<property name="sessionMode" value="native"/>,詳細說明見官方文檔 --> <!-- 這里主要是設置自定義的單Realm應用,若有多個Realm,可使用'realms'屬性代替 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realm" ref="adminRealm" /><property name="cacheManager" ref="cacheManager" /> </bean>adminRealm是你自己實現的授權類
shiro-ehcache.xml:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ehcache.xsd"><!--diskStore:緩存數據持久化的目錄 地址 --><diskStore path="E:\copyrightPlatformehCache" /><defaultCache maxElementsInMemory="1000"maxElementsOnDisk="10000000" eternal="false" overflowToDisk="false"diskPersistent="false" timeToIdleSeconds="120" timeToLiveSeconds="120"diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"></defaultCache></ehcache>diskStore path 也可以定義為: java.io.tmpdir
緩沖清空:
當用戶權限修改后,用戶再次登陸shiro會自動調用realm從數據庫獲取權限數據,如果在修改權限后想立即清除緩存則可以調用realm的clearCache方法清除緩存。
realm中定義clearCached方法:
在權限修改后調用realm中的方法,realm已經由spring管理,所以從spring中獲取realm實例,調用clearCached方法。
作者:試毅_思偉 鏈接:https://www.jianshu.com/p/94028ce76efb 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的shiro整合ehcache的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 选择排序算法,只需这篇文章就够了
- 下一篇: windows ping 不通虚拟机