zoukankan      html  css  js  c++  java
  • 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:developehcache" />
        <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);
        }
  • 相关阅读:
    GC算法 垃圾收集器
    Distinct
    生产者消费者实现
    单例模式(七种实现方法)
    【JUC】JDK1.8源码分析之AbstractQueuedSynchronizer
    【JUC】JDK1.8源码分析之ConcurrentHashMap
    【集合框架】JDK1.8源码分析之HashMap
    一个整数,它加上100后是一个完全平方数, 再加上168又是一个完全平方数,请问该数是多少?
    猴子吃桃问题:猴子第一天摘下若干个桃子, 当即吃了一半,还不过瘾,又多吃了一个; 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。 * 以后每天早上都吃了前一天剩下的一半零一个。 到第10天早上想再吃时,见只剩下一个桃子了。 求第一天共摘了多少。 * 1.程序分析:采取逆向思维的方法,从后往前推断。
    输出9*9口诀 输出9*9乘法表
  • 原文地址:https://www.cnblogs.com/ningheshutong/p/6495411.html
Copyright © 2011-2022 走看看