shiro集成encache
针对多频次或者几乎不变的大数量的数据,我们可以通过缓存来实现,具体的比如说权限认证,这个,每次操作都需要权限认证,所以,这里添加encache注解。具体的认证过程是:
1,用户第一次访问用户权限信息,调用realm来自定义查询数据库,shiro讲权限信息放入到缓存中
2,用户第二次访问用户权限信息的时候,不用查询数据库,直接从缓存中获取授权信息
shiro内部本身就集成了encache,所以,我们默认使用encache来缓存,步骤如下:
1,添加encache依赖
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.5.0</version>
</dependency>
2,添加后,添加shiro-encache.xml文件,如下所示
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<!--diskStore:缓存数据持久化的目录 地址 -->
<diskStore path="d:\develop\ehcache" />
<defaultCache
maxElementsInMemory="1000"
maxElementsOnDisk="10000000"
eternal="false"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>
对这个xml文件的详细解释如下:
name:缓存名称。
maxElementsInMemory:缓存最大个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk:硬盘最大缓存个数。
diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
自动地可用的默认的 Filter 实例是被 DefaultFilter 枚举定义的,枚举的名称字段是可供配置的名称。它们是:
Filter Name |
Class |
anon |
org.apache.shiro.web.filter.authc.AnonymousFilter |
authc |
org.apache.shiro.web.filter.authc.FormAuthenticationFilter |
authcBasic |
org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter |
logout |
org.apache.shiro.web.filter.authc.LogoutFilter |
noSessionCreation |
org.apache.shiro.web.filter.session.NoSessionCreationFilter |
perms |
org.apache.shiro.web.filter.authz.PermissionAuthorizationFilter |
port |
org.apache.shiro.web.filter.authz.PortFilter |
rest |
org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter |
roles |
org.apache.shiro.web.filter.authz.RolesAuthorizationFilter |
ssl |
org.apache.shiro.web.filter.authz.SslFilter |
user |
org.apache.shiro.web.filter.authz.UserFilter |
3,第三步,在springcontext.xml文件中,讲cacheManager bean托管给spring容器,并讲这个bean注入到securityMangere bean中
<!-- 配额本地测试securityManager -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="customRealm" />
<!-- 注入缓存管理器 -->
<property name="cacheManager" ref="cacheManager"/>
</bean> <!-- 缓存管理器 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/>
</bean>
4,第四步,就是运用了,包括两个方面运用
a,controller中的使用
可以通过注解来使用,如下所示,添加注解可用
@RequiresPermissions("user:delete")
b,在前端jsp页面的使用
Jsp页面添加:
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %> 标签名称 标签条件(均是显示标签内容)
<shiro:authenticated> 登录之后
<shiro:notAuthenticated> 不在登录状态时
<shiro:guest> 用户在没有RememberMe时
<shiro:user> 用户在RememberMe时
<shiro:hasAnyRoles name="abc,123" > 在有abc或者123角色时
<shiro:hasRole name="abc"> 拥有角色abc
<shiro:lacksRole name="abc"> 没有角色abc
<shiro:hasPermission name="abc"> 拥有权限资源abc
<shiro:lacksPermission name="abc"> 没有abc权限资源
<shiro:principal> 显示用户身份名称
<shiro:principal property="username"/> 显示用户身份中的属性值
具体的使用如下:
<shiro:hasPermission name="user:delete">
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true"
onclick="deleteItem('dg','deleteUser','userId')">删除用户</a>
</shiro:hasPermission>
5,缓存清空
有三种情况
a,如果用户正常推出,缓存自动清空
b,用户不正常推出,缓存自动清空
c,管理员修改用户权限后,而用户不退出系统,修改权限无法立即生效,这个时候可以手动进行编程实现,即权限修改后,调用realm的clear Cache方法,可以在service中调用自定义realm中的clearCached方法
//清除缓存
public void clearCached() {
PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
super.clearCache(principals);
}
shiro集成encache的更多相关文章
- Apache Shiro 集成-Cas
http://blog.csdn.net/peterwanghao/article/details/8825008 Shiro集成CAS是在1.2版本里新增的功能. Shiro-cas模块将应用作为C ...
- cas+tomcat+shiro实现单点登录-4-Apache Shiro 集成Cas作为cas client端实现
目录 1.tomcat添加https安全协议 2.下载cas server端部署到tomcat上 3.CAS服务器深入配置(连接MYSQL) 4.Apache Shiro 集成Cas作为cas cli ...
- Shiro集成web环境[Springboot]-认证与授权
Shiro集成web环境[Springboot]--认证与授权 在登录页面提交登陆数据后,发起请求也被ShiroFilter拦截,状态码为302 <form action="${pag ...
- spring-boot-2.0.3应用篇 - shiro集成
前言 上一篇:spring-boot-2.0.3源码篇 - 国际化,讲了如何实现国际化,实际上我工作用的模版引擎是freemaker,而不是thymeleaf,不过原理都是相通的. 接着上一篇,这一篇 ...
- Shiro学习笔记四(Shiro集成WEB)
这两天由于家里出了点事情,没有准时的进行学习.今天补上之前的笔记 -----没有学不会的技术,只有不停找借口的人 学习到的知识点: 1.Shiro 集成WEB 2.基于角色的权限控制 3.基于权限的控 ...
- Shiro集成web环境[Springboot]-基础使用
Shiro集成web环境[Springboot] 1.shiro官网查找依赖的jar,其中shiro-ehcache做授权缓存时使用,另外还需要导入ehcache的jar包 <dependenc ...
- Shiro集成SSM基于动态URL权限管理(二)
这个案例基于上一个demo扩展而来.所以数据库表,在Shiro集成SSM基于URL权限管理(一)开篇的一致.如果上个demo操作的建议重新导入一次,避免出现问题. 而这次都不是通过固定写在方法上的注解 ...
- Shiro集成Web
Shiro不仅可以集成到web中,也可以集成Spring. 1.在WEB中添加Shrio支持 2.WEB中INI配置 3.JSP/GSP标签 在WEB中添加Shrio支持 如果要想在web中使用Shr ...
- Java使用reids,以及redis与shiro集成
什么是redis:redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...
随机推荐
- Flash3D学习计划(二)——理解世界,取景,投影变换,并理解投影坐标系
各种坐标系都是有用的,因为某些信息只能在特定场景中才有意义. 一. 世界坐标系 世界坐标系是一个特殊的坐标系,它建立了描述其他坐标系所需要的参考框架.这就意味着,能够用世界坐标系描述其他坐标系的位置, ...
- zlib stream
官方帮助手册 http://www.zlib.net/manual.html #include <stdlib.h> #include <string.h> #incl ...
- linux PHP 安装及 GD库安装
linux GD库安装 GD 安裝 第一部需要做的是先要安裝 GD 到系統內,而安裝 GD 前需要安裝 jpeg-6b, libpng, zlib, freetype.以下是下载网址:GD 2.0.3 ...
- android RelativeLayout控件或view实现叠加效果
在开发android程序的时候,我们经常会遇到让控件或是view实现叠加的效果,一般这种情况,很多人会使用Framelayout来处理,可是有一个问题Framelayout布局在布局上会有很多限制,不 ...
- Excel设置下拉菜单并隐藏下拉菜单来源单元格内容
一.问题来源 做实验室的进展统计表,老师让加上开始时间和完成时间,时间格式:周几_上午(下午.晚上). 这样就可以了做下拉菜单,方便填写,而且格式统一,方便查看. 二.解决办法 2.1 下来菜单 红框 ...
- What is a mocking framework? Why is it useful?
Today I want to talk about mocking frameworks and why they are useful. In order to do that I first n ...
- android在Service中弹出Dialog对话框,即全局性对话框
先说具体做法,原因在其后给出: 写好Alter功能块后,在alter.show()语句前加入: alert.getWindow().setType(WindowManager.LayoutParams ...
- @Component-@Resource-@Repository-@Service-@Controller的区别和理解-------springMVC
1.作用: @Component------------------------泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注.(Component-------成分; 组分; 零件) ...
- Git历险记(一)
[编者按]作为分布式版本控制系统的重要代表——Git已经为越来越多的人所认识,它相对于我们熟悉的CVS.SVN甚至同时分布式控制系统的 Mercurial,有哪些优势和不足呢.这次InfoQ中文站有幸 ...
- JavaScript Best Practices
原文: https://www.w3schools.com/js/js_best_practices.asp --------------------------------------------- ...