栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

七、SpringBoot缓存之EhCache

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

七、SpringBoot缓存之EhCache

七、SpringBoot缓存之EhCache
      • 1、Cache作用
      • 2、使用shiro中默认的EhCache实现缓存
        • 引入依赖
          • 修改ShiroConfig
      • 测试

1、Cache作用
  • Cachehu缓存:计算机内存中一段数据

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

  • 流程:

2、使用shiro中默认的EhCache实现缓存 引入依赖
        
            org.apache.shiro
            shiro-ehcache
            1.5.3
        
修改ShiroConfig

package com.hz52.springboot_jsp_shiro.config;


import com.hz52.springboot_jsp_shiro.shiro.realms.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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;



@Configuration
public class ShiroConfig {

    //1、shiroFilter //负责拦截所有请求(依赖2)
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        //给filter设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);


        //配置系统受限资源
        Map map = new HashMap();
        map.put("/user/login", "anon");   //anon设置为公共资源
        map.put("/user/register", "anon");   //anon设置为公共资源
        map.put("/register.jsp", "anon");   //anon设置为公共资源


        map.put("/**", "authc");  //authc请求这个资源需要认证和授权
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);


        //默认认证界面路径
        shiroFilterFactoryBean.setLoginUrl("/login.jsp");


        //配置系统公共资源


        return shiroFilterFactoryBean;
    }


    //2、创建安全管理器(依赖3)
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();

        //给安全管理器设置
        defaultWebSecurityManager.setRealm(realm);


        return defaultWebSecurityManager;
    }


    //3、创建自定义realm(依赖Realms里面的自定义realm)
    @Bean
    public Realm getRealm() {
        CustomerRealm customerRealm = new CustomerRealm();

        //修改密码匹配器:默认最简单
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        //设置加密算法为md5
        credentialsMatcher.setHashAlgorithmName("md5");
        //设置散列次数
        credentialsMatcher.setHashIterations(1024);

        customerRealm.setCredentialsMatcher(credentialsMatcher);


        //2021/10/23 14:53  开启缓存管理-start
        //1、指定缓存管理器
        customerRealm.setCacheManager(new EhCacheManager());
        //2、开启全局缓存(依赖1)
        customerRealm.setCachingEnabled(true);

        //3、开启认证缓存(依赖2)
        customerRealm.setAuthenticationCachingEnabled(true);
        //在内存中起名(非必要)
        customerRealm.setAuthenticationCacheName("authenticationCache");

        //3、开启授权缓存(依赖2)
        customerRealm.setAuthorizationCachingEnabled(true);
        //在内存中起名(非必要)
        customerRealm.setAuthorizationCacheName("authorizationCache");


        //2021/10/23 14:53  开启缓存管理-end


        return customerRealm;

    }


}

测试

第一次认证会查询,之后不会查询

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/439555.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号