>>分享流行的Java框架以及开源软件,对孙卫琴的《精通Spring:Java Web开发技术详解》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 20412 个阅读者 刷新本主题
 * 贴子主题:  Spring Boot集成shiro ehcache(使用shiro的缓存管理) 回复文章 点赞(0)  收藏  
作者:sunshine    发表时间:2020-03-08 18:42:36     消息  查看  搜索  好友  邮件  复制  引用

          

Spring Boot集成shiro ehcache(使用shiro的缓存管理)

一、项目准备

     为了方便,这里直接使用Spring Boot集成shiro章节的源码。    

二、添加依赖

   <!-- cache -->
< dependency>
     < groupId>org.springframework.boot </ groupId>
     < artifactId>spring-boot-starter-cache </ artifactId>
</ dependency>
< dependency>
     < groupId>net.sf.ehcache </ groupId>
     < artifactId>ehcache </ artifactId>
</ dependency>
<!-- shiro-ehcache-->
< dependency>
     < groupId>org.apache.shiro </ groupId>
     < artifactId>shiro-ehcache </ artifactId>
     < version>1.4.0 </ version>
</ dependency>

三、创建ehcache配置文件

     在resources目录下,创ehcache.xml配置文件,内容如下:                

    <?xml version="1.0" encoding="UTF-8"?>
< ehcache  updateCheck= "false"  dynamicConfig= "false">
     < diskStore  path= "java.io.tmpdir"/>

     < cache  name= "users"
            timeToLiveSeconds= "300"
            maxEntriesLocalHeap= "1000"/>

     <!--
        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:内存数量最大时是否清除。
    -->
     < defaultCache  name= "defaultCache"
                   maxElementsInMemory= "10000"
                   eternal= "false"
                   timeToIdleSeconds= "120"
                   timeToLiveSeconds= "120"
                   overflowToDisk= "false"
                   maxElementsOnDisk= "100000"
                   diskPersistent= "false"
                   diskExpiryThreadIntervalSeconds= "120"
                   memoryStoreEvictionPolicy= "LRU"/>
</ ehcache>

四、修改ShiroConfig类

     1、添加以下代码:                

   /**
* 缓存管理器
*  @return
*/

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

/**
* aop代理
*  @return
*/

@Bean
@DependsOn( "lifecycleBeanPostProcessor")
public DefaultAdvisorAutoProxyCreator  defaultAdvisorAutoProxyCreator() {
    DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator =  new DefaultAdvisorAutoProxyCreator();
    defaultAdvisorAutoProxyCreator.setProxyTargetClass( true);
     return defaultAdvisorAutoProxyCreator;
}

     2、修改userRealm()和securityManager(),添加缓存支持                

  /**
* 自定义realm
*
*  @return
*/

@Bean
public UserRealm  userRealm() {
    UserRealm userRealm =  new UserRealm();
    userRealm.setCredentialsMatcher(hashedCredentialsMatcher());
    userRealm.setCacheManager(ehCacheManager());
     return userRealm;
}

/**
* 安全管理器
* 注:使用shiro-spring-boot-starter 1.4时,返回类型是SecurityManager会报错,直接引用shiro-spring则不报错
*
*  @return
*/

@Bean
public DefaultWebSecurityManager  securityManager() {
    DefaultWebSecurityManager securityManager =  new DefaultWebSecurityManager();
    securityManager.setRealm(userRealm());
    securityManager.setCacheManager(ehCacheManager());
     return securityManager;
}

五、添加EhcacheConfig类

     在com.songguoliang.springboot.configuration包下添加EhcacheConfig配置类:                

  package com.songguoliang.springboot.configuration;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
*  @Description
*  @Author sgl
*  @Date 2018-06-15 11:45
*/

@Configuration
public   class  EhcacheConfig {
      /**
     * 设置为共享模式
     *  @return
     */

     @Bean
     public EhCacheManagerFactoryBean  ehCacheManagerFactoryBean() {
        EhCacheManagerFactoryBean cacheManagerFactoryBean =  new EhCacheManagerFactoryBean();
        cacheManagerFactoryBean.setShared( true);
         return cacheManagerFactoryBean;
    }
}

六、修改UserRealm类,添加@Lazy注解延迟注入service

  //省略代码……

/**
* 注意此处需要添加@Lazy注解,否则SysUserService缓存注解、事务注解不生效
*/

@Autowired
@Lazy
private SysUserService sysUserService;

@Autowired
@Lazy
private SysPermissionService sysPermissionService;

//省略代码……

七、注意

     至此,我们以将shiro和ehcache集成完毕,使用的是shiro框架提供的缓存管理器,有个需要特别注意的是, UserRealm里注入的SysUserService等service,需要延迟注入,所以都要添加@Lazy注解(如果不加需要自己延迟注入),否则会导致该service里的@Cacheable缓存注解、@Transactional事务注解等失效

                                          源码:  

github  

码云
                                    
                                                                    
----------------------------
原文链接:https://blog.csdn.net/gnail_oug/article/details/80705718

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 flybird 在 2020-03-10 10:18:00 重新编辑]
  Java面向对象编程-->图形用户界面(上)
  JavaWeb开发-->Web运作原理(Ⅰ)
  JSP与Hibernate开发-->使用JPA和注解
  Java网络编程-->Java反射机制
  精通Spring-->绑定表单
  Vue3开发-->Vue组件开发高级技术
  Marshalling在Netty中的使用
  Redis 超详细总结笔记
  RocketMQ-Spring 为什么能成为 Spring 生态中最受欢迎的
  说一下Spring @Autowired 注解自动注入流程
  如何编写优雅的Spring架构API
  微服务的拆分方式
  Spring MVC服务器端推送的两种方式
  使用Spring MVC处理404错误的方法
  springMVC:HandlerInterceptor拦截器的使用
  springboot集成通用mapper实现Echarts
  中央厨房订单管理系统,引入ActiveMQ消息队列,平滑处理高峰...
  SpringBoot集成Quartz实现定时任务和调度
  Spring如何实现AOP,请不要再说cglib了!
  spring整合WebService入门详解
  大话微服务」深入聊聊SpringCloud之客户端负载均衡机制
  更多...
 IPIP: 已设置保密
楼主      
该用户目前不在线 nihaota 
  
威望: 0
级别: 新手上路
魅力: 1315
经验: 1315
现金: 2944
发文章数: 243
注册时间: 0001-01-01
 消息  查看  搜索  好友  邮件  复制  引用


讨债公司
发文章时间 2022-10-28 19:57:59
 IPIP: 已设置保密 1 楼     
1页 1条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。