博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shiro实现本地内存Ehcache实现将菜单权限进行缓存
阅读量:4287 次
发布时间:2019-05-27

本文共 4086 字,大约阅读时间需要 13 分钟。

一 为何要使用缓存

我们在认证通过进行授权,执行页面的hasRoles(xxx) ,isPermited(xxx) 这些语句时,会调用底层中自定义reaml中的doGetAutorizationInfo(xxxxxx)方法

我们使用ljf账户模拟登陆:

index.jsp页面:

2.使用admin进行登录

index.jsp页面:

3.查看index.jsp页面

查看后端调用:和前端页面调用标签一一对应,ljf的角色为user,则调用7次,admin的角色为admin,则调用8次

 二.使用缓存

2.1 缓存的作用

  • Cache 缓存: 计算机内存中一段数据

  • 作用: 用来减轻DB的访问压力,从而提高系统的查询效率

2.2 缓存执行流程

2.3 配置流程

1.pom文件:

org.apache.shiro
shiro-ehcache
1.5.3

2.在reaml中设置缓存代码

package com.shiro.ljf.demo.sptshirodemo.config;import com.shiro.ljf.demo.sptshirodemo.shiro.CustomerRealm;import org.apache.shiro.authc.credential.HashedCredentialsMatcher;import org.apache.shiro.cache.ehcache.EhCacheManager;import org.apache.shiro.realm.Realm;import org.apache.shiro.spring.web.ShiroFilterFactoryBean;import org.apache.shiro.web.mgt.DefaultWebSecurityManager;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.LinkedHashMap;import java.util.Map;/** * @ClassName: ShiroConfig * @Description: TODO   整合springboot的shiro配置类 * @Author: liujianfu * @Date: 2020/10/28 15:11:16  * @Version: V1.0 **/@Configurationpublic class ShiroConfig {    //1.创建shiroFilter :负责拦截所有请求    @Bean    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){        System.out.println("step3:>>进入过滤器");        //创建shiro的filter,配置shiroFilterFactoryBean        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();        //注入安全管理器        shiroFilterFactoryBean.setSecurityManager(securityManager);        //设置过滤资源        Map
map = new LinkedHashMap<>(); //map.put("/**","authc"); /** 代表拦截项目中一切资源 authc 代表shiro中的一个filter的别名,详细内容看文档的shirofilter列表**/ //map.put("/index.jsp","authc");//验证资源 // map.put("/index*","anon");//验证资源 anon 匿名访问,可以跳转到index要访问的controller //设置公共资源 ,anno过滤器,设置访问公共资源,放在前面 map.put("/user/login","anon"); map.put("/user/register","anon");//anon 设置为公共资源 放行资源放在前面 map.put("/register.jsp","anon");//anon 设置为公共资源 放行资源放在前面 //设置受限资源 map.put("/index*","authc");//验证资源,受限资源,跳转登录login.jsp页面 //默认认证跳转路径 shiroFilterFactoryBean.setLoginUrl("/login.jsp"); shiroFilterFactoryBean.setFilterChainDefinitionMap(map); return shiroFilterFactoryBean; } //2.创建安全管理器 @Bean(name="securityManager") public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") Realm realm){ System.out.println("step2:>> 进入securityManager方法"); DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager(); //给安全管理器设置 defaultWebSecurityManager.setRealm(realm); return defaultWebSecurityManager; } //3.创建自定义realm @Bean(name="userRealm") public Realm getRealm(){ System.out.println("step1:>>进入realm方法"); //return new CustomerRealm(); CustomerRealm customerRealm = new CustomerRealm(); //修改凭证校验匹配器 HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher(); //设置加密算法为md5 credentialsMatcher.setHashAlgorithmName("MD5"); //设置散列次数 credentialsMatcher.setHashIterations(1024); customerRealm.setCredentialsMatcher(credentialsMatcher); /** //开启缓存管理 **/ customerRealm.setCacheManager(new EhCacheManager()); customerRealm.setCachingEnabled(true);//开启全局缓存 customerRealm.setAuthenticationCachingEnabled(true);//认证认证缓存 customerRealm.setAuthenticationCacheName("my-authenticationCache"); customerRealm.setAuthorizationCachingEnabled(true);//开启授权缓存 customerRealm.setAuthorizationCacheName("my-authorizationCache"); return customerRealm; }}

2.4 调试

1.使用ljf账号,输入:http://localhost:8888/shiro/index  登录成功后,使劲刷新页面

可以看到后端只加载了一次授权方法

2.使用admin账号,输入:http://localhost:8888/shiro/index  登录成功后,使劲刷新页面 

使劲刷新页面,后端提示,只加载了一次授权方法:

 结论:是使用缓存Ehacache,实现了缓存开启功能!

转载地址:http://datgi.baihongyu.com/

你可能感兴趣的文章
CentOS-7搭建Gitlab服务器
查看>>
软件架构设计原则(我们为什么要学习)
查看>>
eclipse 的mybatis中mapper.xml文件标签没有提示的解决方法
查看>>
linux 上一主两从mysql集群中某台数据库宕机解决方法
查看>>
大牛面试指南
查看>>
android入门(一)---UI组件之文本框(TextView)
查看>>
演示动画怎么实现的
查看>>
android入门---Activity组件.活动(一)
查看>>
Android入门---GridView组件
查看>>
获取apk文件上的精美图片素材
查看>>
RelativeLayout中Margin属性
查看>>
JAVA中文乱码解决方法
查看>>
端口号占用问题 serveral ports(8080,8009) are already in use
查看>>
Button中使用颜色控制按钮点击时的形状和颜色
查看>>
Android入门---ImageView(图像视图)
查看>>
浅析JAVA的抽象和接口
查看>>
Android入门----Switch控件
查看>>
ProgressBar控件入门
查看>>
SeekBar控件入门
查看>>
DatePicker和TimePicker入门
查看>>