Spring Boot教程(十九):Spring Boot集成shiro ehcache(使用shiro的缓存管理)

一、项目准备

为了方便,这里直接使用Spring Boot教程(十六):Spring Boot集成shiro章节的源码。

二、添加依赖


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-cacheartifactId>
dependency>
<dependency>
    <groupId>net.sf.ehcachegroupId>
    <artifactId>ehcacheartifactId>
dependency>

<dependency>
    <groupId>org.apache.shirogroupId>
    <artifactId>shiro-ehcacheartifactId>
    <version>1.4.0version>
dependency>

三、创建ehcache配置文件

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


<ehcache updateCheck="false" dynamicConfig="false">
    <diskStore path="java.io.tmpdir"/>

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

    
    <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
码云

你可能感兴趣的:(springboot,ehcache,shiro,Spring,Boot学习笔记)