springboot中使用缓存shiro-ehcache

在pom.xml中注入缓存依赖,版本(Sep 09, 2016)
spring-context-support
包含支持UI模版(Velocity,FreeMarker,JasperReports),
邮件服务,
脚本服务(JRuby),
缓存Cache(EHCache),
任务计划Scheduling(uartz)。


    org.apache.shiro
    shiro-ehcache
    1.3.2


    org.springframework
    spring-context-support
    4.3.7.RELEASE

在ShiroConfiguration中注入缓存

/**
     * shiro缓存管理器;
     * 需要注入对应的其它的实体类中:
     * 1、安全管理器:securityManager
     * 可见securityManager是整个shiro的核心;
     * @return
     */
    @Bean
    public EhCacheManager ehCacheManager(){
        System.out.println("ShiroConfiguration.getEhCacheManager()");
        EhCacheManager cacheManager = new EhCacheManager();
        cacheManager.setCacheManagerConfigFile("classpath:config/ehcache-shiro.xml");
        return cacheManager;
    }

将缓存对象注入到SecurityManager中:

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

添加缓存配置文件:
在src/main/resouces/config添加ehcache-shiro.xml配置文件:




    

    
    


    
    
    


在配置文件上已经有很详细的解释了,所以这里就过多介绍ehcache的配置了。
运行程序访问:http://127.0.0.1:8080/userInfo/userAdd
查看控制台的打印信息:
权限配置-->MyShiroRealm.doGetAuthorizationInfo()
这个信息就只打印一次了,说明我们的缓存生效了。

文章主要参考于作者林祥纤的博客

http://412887952-qq-com.iteye.com/blog/2299780

你可能感兴趣的:(springboot中使用缓存shiro-ehcache)