shiro实现本地内存Ehcache实现将菜单权限进行缓存

一 为何要使用缓存

我们在认证通过进行授权,执行页面的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文件:

 <!--引入shiro和ehcache-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>1.5.3</version>
        </dependency>

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
 **/
@Configuration
public 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<String,String> 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,实现了缓存开启功能!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值