Skip to content

Commit

Permalink
shiro整合ehcache 录制完毕
Browse files Browse the repository at this point in the history
  • Loading branch information
leechenxiang committed Oct 27, 2017
1 parent 1e34395 commit b62ce4a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.itzixi.common.utils.LeeJSONResult;
import com.itzixi.web.shiro.ShiroDBRealm;

/**
*
* @Title: CenterController.java
Expand All @@ -22,6 +27,9 @@
@RequestMapping("shiroTest")
public class ShiroTestController extends BaseController {

@Autowired
private ShiroDBRealm realm;

@RequestMapping("/shiroPage")
@RequiresPermissions(value = {"company:mng", "appuser:check", "company:check"}, logical = Logical.OR)
public ModelAndView index() {
Expand All @@ -35,4 +43,23 @@ public ModelAndView index() {

return modelAndView;
}

/**
*
* @Title: ShiroTestController.java
* @Package com.itzixi.web.controller
* @Description: 强制清除缓存
* Copyright: Copyright (c) 2017
* Company:FURUIBOKE.SCIENCE.AND.TECHNOLOGY
*
* @author leechenxiang
* @date 2017年10月27日 下午8:37:20
* @version V1.0
*/
@RequestMapping("/clearCache")
@ResponseBody
public LeeJSONResult clearCache() {
realm.clearCache();
return LeeJSONResult.ok();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
Expand All @@ -11,10 +12,10 @@
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itzixi.pojo.ActiveUser;
import com.itzixi.pojo.SysPermission;
Expand All @@ -33,6 +34,7 @@
* @date 2017年10月9日 下午8:32:37
* @version V1.0
*/
@Component
public class ShiroDBRealm extends AuthorizingRealm {

@Autowired
Expand Down Expand Up @@ -109,5 +111,16 @@ protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal
return simpleAuthorizationInfo;
}

/**
*
* @Description: 权限修改生效后,立即刷新清空缓存,则可以实现用户不退出生效新的权限
*
* @author leechenxiang
* @date 2016年9月29日 下午9:34:07
*/
public void clearCache() {
PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
super.clearCache(principals);
}
}

43 changes: 43 additions & 0 deletions leecx-mng/leecx-mng-web/src/main/resources/shiro/ehcache-shiro.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="shiroCache">

<diskStore path="C:\shiro\ehcache" />
<!-- <diskStore path="java.io.tmpdir"/> -->

<!--
eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
maxElementsInMemory:缓存中允许创建的最大对象数
overflowToDisk:内存不足时,是否启用磁盘缓存。
timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前, 两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是 0 就意味着元素可以停顿无穷长的时间。
timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。
diskPersistent:设定在虚拟机重启时是否进行磁盘存储,默认为false
diskExpiryThreadIntervalSeconds: 属性可以设置该线程执行的间隔时间(默认是120秒,不能太小
1 FIFO,先进先出
2 LFU,最少被使用,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
3 LRU,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>

<cache name="activeSessionCache"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="false"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="600"/>

<cache name="shiro.authorizationCache"
maxElementsInMemory="100"
eternal="false"
timeToLiveSeconds="600"
overflowToDisk="false"/>

</ehcache>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

<!-- 扫描包加载Service实现类 -->
<context:component-scan base-package="com.itzixi.service,com.itzixi.web.utils"></context:component-scan>
<context:component-scan base-package="com.itzixi.service,com.itzixi.web.utils,com.itzixi.web.shiro"></context:component-scan>

<!-- 自动创建代理 对@AspectJ注解的支持 -->
<!-- 通知spring使用cglib而不是jdk的来生成代理方法 AOP可以拦截到Service -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="shiroDbRealm"/>
<!-- 设置缓存管理器为 ehcache -->
<property name="cacheManager" ref="shiroEhcacheManager"></property>
</bean>

<!-- 项目自定义的Realm -->
Expand Down Expand Up @@ -57,5 +59,9 @@
<!-- AOP式方法级权限检查 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>

<!-- 用户授权信息Cache, 采用EhCache -->
<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:shiro/ehcache-shiro.xml"/>
</bean>

</beans>

0 comments on commit b62ce4a

Please sign in to comment.