• shiro:缓存管理


    1、

    书写打印语句,运行查看打印的次数,从而获取到代码执行的次数:

        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
            //获取用户的用户名,跟doGetAuthenticationInfo方法中的new SimpleAuthenticationInfo参数是对应的
            String username= (String) principalCollection.iterator().next();
            //根据用户名查询当前用户的前台列表
            Set<String> roleNames=roleDao.queryRoleNameByUsername(username);
            Set<String> ps=permissionDao.queryPermissionsByUsername(username);
            SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
            info.setRoles(roleNames);
            info.setStringPermissions(ps);
            System.out.println("查询用户及权限信息");
            return info;
        }

    从打印结果来看,代码执行了多次,使用Shiro进行权限管理过程中,每次授权都会访问realm中的doGetAuthorizationInfo方法查询当前用户的角色及权限信息,如果系统的用户量比较大则会对教据库造威比较大的压力(会对所有的权限进行一次比较)

    2020-09-29 15:22:04.150  INFO 11984 --- [           main] com.zhb.test.DemoApplication             : Started DemoApplication in 6.44 seconds (JVM running for 7.9)
    2020-09-29 15:22:09.717  INFO 11984 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2020-09-29 15:22:09.717  INFO 11984 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
    2020-09-29 15:22:09.732  INFO 11984 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 15 ms
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@718a383c] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: select * from tb_users where username=? 
    ==> Parameters: zhai(String)
    <==    Columns: user_id, username, password, password_salt
    <==        Row: 1, zhai, ae1de49a9f24d38da38f7afe9ce70db4, 68795
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@718a383c]
    成功
    zhai123
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@143272b] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT role_name FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: role_name
    <==        Row: 学生
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@143272b]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@394df695] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT tb_permissions.permission_code FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id INNER JOIN tb_rps ON tb_roles.role_id = tb_rps.rid INNER JOIN tb_permissions ON tb_rps.pid = tb_permissions.permission_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: permission_code
    <==        Row: stu_select
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@394df695]
    查询用户及权限信息
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@26d7596d] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT role_name FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: role_name
    <==        Row: 学生
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@26d7596d]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@103bb341] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT tb_permissions.permission_code FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id INNER JOIN tb_rps ON tb_roles.role_id = tb_rps.rid INNER JOIN tb_permissions ON tb_rps.pid = tb_permissions.permission_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: permission_code
    <==        Row: stu_select
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@103bb341]
    查询用户及权限信息
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7436aff4] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT role_name FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: role_name
    <==        Row: 学生
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7436aff4]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@79afc66a] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT tb_permissions.permission_code FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id INNER JOIN tb_rps ON tb_roles.role_id = tb_rps.rid INNER JOIN tb_permissions ON tb_rps.pid = tb_permissions.permission_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: permission_code
    <==        Row: stu_select
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@79afc66a]
    查询用户及权限信息
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6430afc2] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT role_name FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: role_name
    <==        Row: 学生
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6430afc2]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@49d3c55e] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT tb_permissions.permission_code FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id INNER JOIN tb_rps ON tb_roles.role_id = tb_rps.rid INNER JOIN tb_permissions ON tb_rps.pid = tb_permissions.permission_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: permission_code
    <==        Row: stu_select
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@49d3c55e]
    查询用户及权限信息
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1db031b7] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT role_name FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: role_name
    <==        Row: 学生
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1db031b7]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c691bd4] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT tb_permissions.permission_code FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id INNER JOIN tb_rps ON tb_roles.role_id = tb_rps.rid INNER JOIN tb_permissions ON tb_rps.pid = tb_permissions.permission_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: permission_code
    <==        Row: stu_select
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c691bd4]
    查询用户及权限信息
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@669b4a4a] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT role_name FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: role_name
    <==        Row: 学生
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@669b4a4a]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@477cdf24] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT tb_permissions.permission_code FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id INNER JOIN tb_rps ON tb_roles.role_id = tb_rps.rid INNER JOIN tb_permissions ON tb_rps.pid = tb_permissions.permission_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: permission_code
    <==        Row: stu_select
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@477cdf24]
    查询用户及权限信息
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5617431d] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT role_name FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: role_name
    <==        Row: 学生
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5617431d]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@247ce7cd] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT tb_permissions.permission_code FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id INNER JOIN tb_rps ON tb_roles.role_id = tb_rps.rid INNER JOIN tb_permissions ON tb_rps.pid = tb_permissions.permission_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: permission_code
    <==        Row: stu_select
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@247ce7cd]
    查询用户及权限信息
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4a94583f] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT role_name FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: role_name
    <==        Row: 学生
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4a94583f]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6775981e] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6b0efb9] will not be managed by Spring
    ==>  Preparing: SELECT tb_permissions.permission_code FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id INNER JOIN tb_rps ON tb_roles.role_id = tb_rps.rid INNER JOIN tb_permissions ON tb_rps.pid = tb_permissions.permission_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: permission_code
    <==        Row: stu_select
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6775981e]
    查询用户及权限信息


    2、缓存的使用

    Shiro支持缓存以降低对数据库的访问压力〔缓存的是授权信息)

    (1)导入依赖

     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>mybatis-spring-boot-cache</artifactId>
            </dependency>
            <!--第三方缓存的支持-->
            <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
            </dependency>
            <!--shiro对缓存的支持-->
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-ehcache</artifactId>
                <version>1.4.0</version>
     </dependency>

    (2)配置缓存策略

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache updateCheck="false" dynamicConfig="false">
        <diskStore path="C:	oolsehcache" />
        <defaultCache name="defaultCache"
                maxElementsInMemory="1000"
                eternal="false"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                overflowToDisk="false"
                maxElementsOnDisk="100000"
                diskPersistent="false"
                diskExpiryThreadIntervalSeconds="120"
                memoryStoreEvictionPolicy="LRU">
        </defaultCache>
    </ehcache>
    • diskStore:缓存数据持久化的目录
    • eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
    • maxElementsInMemory:缓存中允许创建的最大对象数
    • overflowToDisk:内存不足时,是否启用磁盘缓存。
    • timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前, 两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是 0 就意味着元素可以停顿无穷长的时间。
    • timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
    • memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。
    • diskPersistent:设定在虚拟机重启时是否进行磁盘存储,默认为false
    • diskExpiryThreadIntervalSeconds: 属性可以设置该线程执行的间隔时间(默认是120秒,不能太小

    FIFO,先进先出
    LFU,最少被使用,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
    LRU,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。

    (3)配置缓存管理器

    配置缓存管理器:

       @Bean
        public EhCacheManager getEhCacheManager(){
            EhCacheManager ehCacheManager=new EhCacheManager();
            ehCacheManager.setCacheManagerConfigFile("classpath:ehcache.xml");
            return ehCacheManager;
        }

    将缓存管理器交给SecurityManager:

       @Bean//安全管理器
        public DefaultWebSecurityManager getDefaultWebSecurityManager(MyRealm myRealm,EhCacheManager ehCacheManager) {
            DefaultWebSecurityManager defaultSecurityManager = new DefaultWebSecurityManager();
            defaultSecurityManager.setRealm(myRealm);//SecurityManager完成校验需要realm
            defaultSecurityManager.setCacheManager(ehCacheManager);
            return defaultSecurityManager;
        }

    (4)测试

    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@37f849de]
    成功
    zhai123
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5e7d8fd8] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@2b0be8da] will not be managed by Spring
    ==>  Preparing: SELECT role_name FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: role_name
    <==        Row: 学生
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5e7d8fd8]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@8ed5890] was not registered for synchronization because synchronization is not active
    JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@2b0be8da] will not be managed by Spring
    ==>  Preparing: SELECT tb_permissions.permission_code FROM tb_users INNER JOIN tb_urs ON tb_users.user_id = tb_urs.uid INNER JOIN tb_roles ON tb_urs.rid = tb_roles.role_id INNER JOIN tb_rps ON tb_roles.role_id = tb_rps.rid INNER JOIN tb_permissions ON tb_rps.pid = tb_permissions.permission_id WHERE tb_users.username=? 
    ==> Parameters: zhai(String)
    <==    Columns: permission_code
    <==        Row: stu_select
    <==      Total: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@8ed5890]
    查询用户及权限信息
  • 相关阅读:
    IE下判断IE版本的语句...[if lte IE 6]……[endif]
    “浏览器模式”和“文档模式”
    IoC框架---通俗概述
    Castle IOC容器组件生命周期管理
    Castle学习笔记----初探IOC容器
    Castle IOC容器内幕故事(下)
    Castle IOC容器内幕故事(上)
    Castle IOC容器构建配置详解(二)
    javascript属性一览
    javascript addEventListener方法
  • 原文地址:https://www.cnblogs.com/zhai1997/p/13750509.html
Copyright © 2020-2023  润新知