shiro自带的MemoryConstrainedCacheManager作缓存只能用于本机,那么在集群时就无法使用,先更为ehcache。
再次之前,修改pom:
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-ehcache --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.2.2</version> </dependency>
1.首先修改spring-shiro.xml
<!-- 缓存管理 -->
<!--<bean id="shiroCacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean>-->
<!-- 缓存管理器ehcache -->
<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:spring/shiro-ehcache.xml"/>
</bean>
全部修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<!-- 导入数据库的相关配置 -->
<import resource="classpath:spring/spring-database.xml"/>
<!-- 对应于web.xml中配置的那个shiroFilter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- Shiro的核心安全接口,这个属性是必须的 -->
<property name="securityManager" ref="securityManager"/>
<!-- 要求登录时的链接(登录页面地址),非必须的属性,默认会自动寻找Web工程根目录下的"/login.jsp"页面 -->
<property name="loginUrl" value="/base/Shiro/login.html"/>
<!-- 登录成功后要跳转的连接(本例中此属性用不到,因为登录成功后的处理逻辑在LoginController里硬编码) -->
<!-- <property name="successUrl" value="/" ></property> -->
<!-- 用户访问未对其授权的资源时,所显示的连接 -->
<!--<property name="unauthorizedUrl" value="/error/unauthorized"/>-->
<!--<property name="filterChainDefinitions">-->
<!--<value>-->
<!--/admin/**=authc-->
<!--</value>-->
<!--</property>-->
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>
<!-- 数据库保存的密码是使用MD5算法加密的,所以这里需要配置一个密码匹配对象 -->
<!--<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.Md5CredentialsMatcher"></bean>-->
<!-- 缓存管理 -->
<!--<bean id="shiroCacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean>-->
<!-- 缓存管理器ehcache -->
<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:spring/shiro-ehcache.xml"/>
</bean>
<!--
使用Shiro自带的JdbcRealm类
指定密码匹配所需要用到的加密对象
指定存储用户、角色、权限许可的数据源及相关查询语句
-->
<bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<!--<property name="credentialsMatcher" ref="credentialsMatcher"></property>-->
<property name="permissionsLookupEnabled" value="true"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="authenticationQuery"
value="SELECT password FROM base_user WHERE login_name = ?"></property>
<property name="userRolesQuery"
value="SELECT t.role_code from base_role t left join base_user tt on t.ROLE_CODE=tt.DEFAULT_ROLE_CODE WHERE LOGIN_NAME = ? "></property>
<property name="permissionsQuery"
value="SELECT PT.PERMISSION_NAME from PERMISSION_WITH pw LEFT JOIN PERMISSION_TEST pt on pw.P_ID = pt.PERMISSION_ID LEFT JOIN BASE_ROLE t on t.ROLE_ID = pw.ROLE_ID where t.role_code = ?"></property>
</bean>
<!-- Shiro安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--获取权限源,可以通过获取数据库也可以redis-->
<property name="realm" ref="jdbcRealm"></property>
<property name="cacheManager" ref="shiroCacheManager"></property>
</bean>
<!-- Shiro的注解配置一定要放在spring-mvc中 -->
</beans>
2.添加shiro-ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="shiroCache">
<!--
name:缓存名称。
maxElementsInMemory:缓存最大数目
maxElementsOnDisk:硬盘最大缓存个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
overflowToDisk:是否保存到磁盘,当系统当机时
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
memoryStoreEvictionPolicy:
Ehcache的三种清空策略;
FIFO,first in first out,这个是大家最熟的,先进先出。
LFU, Less Frequently Used,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
LRU,Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<!-- 登录记录缓存锁定10分钟 -->
<!--<cache name="passwordRetryCache"-->
<!--maxEntriesLocalHeap="2000"-->
<!--eternal="false"-->
<!--timeToIdleSeconds="3600"-->
<!--timeToLiveSeconds="0"-->
<!--overflowToDisk="false"-->
<!--statistics="true">-->
<!--</cache>-->
</ehcache>
路径如下:
当然,你也可以放在这里,这样就更美观一点:
因为在target下,这些配置的路径都在spring下,如图:
3.测试:
@Autowired
private CacheManager shiroCacheManager;
@RequestMapping(value="/clearShiroCatch",method= RequestMethod.GET)
@ResponseBody
public void clearShiroCatch(){
//获取权限看一下
/*
/* @Autowired
private CacheManager shiroCacheManager;
*/
Cache<Object, Object> cache = shiroCacheManager.getCache("org.apache.shiro.realm.jdbc.JdbcRealm.authorizationCache");
// shiroCacheManager.destroy();//清除全部缓存
// LifecycleUtils.destroy(cache);//清除某个缓存
Subject subject = SecurityUtils.getSubject();
/*subject.getPrincipal()------>登录名
String realmName = subject.getPrincipals().getRealmNames().iterator().next();
//第一个参数为用户名,想要操作权限的用户,第二个参数为realmName,
SimplePrincipalCollection principals = new SimplePrincipalCollection(subject.getPrincipal(),realmName);
*/
RealmSecurityManager securityManager =
(RealmSecurityManager) SecurityUtils.getSecurityManager();
JdbcRealm jdbcRealm = (JdbcRealm)securityManager.getRealms().iterator().next();
//删除登陆人
jdbcRealm.getAuthorizationCache().remove(subject.getPrincipal());
//删除登陆人对应的缓存
jdbcRealm.getAuthorizationCache().remove(subject.getPrincipals());
//重新加载subject
subject.releaseRunAs();
}
同时:在从idea的ssm移植到eclipse的ssh下时,出现了2个问题:
1.shiro-ehcache修改头部:
<?xml version="1.0" encoding="GB2312"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false" name="shiroCache">
<!--
name:缓存名称。
maxElementsInMemory:缓存最大数目
maxElementsOnDisk:硬盘最大缓存个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
overflowToDisk:是否保存到磁盘,当系统当机时
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
memoryStoreEvictionPolicy:
Ehcache的三种清空策略;
FIFO,first in first out,这个是大家最熟的,先进先出。
LFU, Less Frequently Used,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
LRU,Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<!-- 登录记录缓存锁定10分钟 -->
<!--<cache name="passwordRetryCache"-->
<!--maxEntriesLocalHeap="2000"-->
<!--eternal="false"-->
<!--timeToIdleSeconds="3600"-->
<!--timeToLiveSeconds="0"-->
<!--overflowToDisk="false"-->
<!--statistics="true">-->
<!--</cache>-->
</ehcache>
2.由于老项目的编码是gbk编码,修改头部编码GB2312