010-shiro與spring web項目整合【四】緩衝Ehcache、Redis

來源:互聯網
上載者:User

標籤:red   manager   www   splay   持久化   sch   清除緩衝   over   自動   

一、Ehcache

shiro每次授權都會通過realm擷取許可權資訊,為了提高訪問速度需要添加緩衝,第一次從realm中讀取許可權資料,之後不再讀取,這裡Shiro和Ehcache整合。

1、添加Ehcache的jar包
        <dependency>            <groupId>net.sf.ehcache</groupId>            <artifactId>ehcache-core</artifactId>            <version>2.5.3</version>        </dependency>        <dependency>            <groupId>org.apache.shiro</groupId>            <artifactId>shiro-ehcache</artifactId>            <version>1.3.2</version>        </dependency>
View Code2、配置cacheManager

在applicationContext-shiro.xml中配置緩衝管理器。

    <!-- securityManager安全管理器 -->    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">        <property name="realm" ref="customRealm"/>        <!-- 注入緩衝管理器 -->        <property name="cacheManager" ref="cacheManager"/>        <!-- 注入session管理器 -->        <property name="sessionManager" ref="sessionManager"/>        <!-- 記住我 -->        <property name="rememberMeManager" ref="rememberMeManager"/>    </bean>    <!-- 緩衝管理器 -->    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">        <property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/>    </bean>
3、配置shiro-ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">    <!--diskStore:快取資料持久化的目錄 地址  -->    <diskStore path="F:\develop\ehcache" />    <defaultCache         maxElementsInMemory="1000"         maxElementsOnDisk="10000000"        eternal="false"         overflowToDisk="false"         diskPersistent="false"        timeToIdleSeconds="120"        timeToLiveSeconds="120"         diskExpiryThreadIntervalSeconds="120"        memoryStoreEvictionPolicy="LRU">    </defaultCache></ehcache>
View Code4、清空緩衝

  當使用者權限修改後,使用者再次登陸shiro會自動調用realm從資料庫擷取許可權資料,如果在修改許可權後想立即清除緩衝則可以調用realm的clearCache方法清除緩衝。

  realm中定義clearCached方法:

    //清除緩衝    public void clearCached() {        PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();        super.clearCache(principals);    }

  在許可權修改後調用realm中的方法,realm已經由spring管理,所以從spring中擷取realm執行個體,調用clearCached方法。

010-shiro與spring web項目整合【四】緩衝Ehcache、Redis

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.