zoukankan      html  css  js  c++  java
  • 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>

    diskStorepath 也可以定义为:java.io.tmpdir

    你可能需要配置Ehcache提示信息


    缓冲清空:

    当用户权限修改后,用户再次登陆shiro会自动调用realm从数据库获取权限数据,如果在修改权限后想立即清除缓存则可以调用realmclearCache方法清除缓存。
    realm中定义clearCached方法:
    // 清除缓存
    	public void clearCached() {
    		PrincipalCollection principals = SecurityUtils.getSubject()
    				.getPrincipals();
    		super.clearCache(principals);
    	}

    在权限修改后调用realm中的方法,realm已经由spring管理,所以从spring中获取realm实例,调用clearCached方法。

  • 相关阅读:
    C#深入浅出 修饰符(二)
    HDU 5785 Interesting
    HDU 5783 Divide the Sequence
    HDU 5781 ATM Mechine
    UVA 714 Copying Books
    uva 1471 Defense Lines
    UVA 11134 Fabled Rooks
    UVA 11572 Unique Snowflakes
    UVA 11093 Just Finish it up
    UVA 10954 Add All
  • 原文地址:https://www.cnblogs.com/zhousiwei/p/10625843.html
Copyright © 2011-2022 走看看