1、在applicationContext-redis.xml配置文件中增加如下: 申明一个cacheManager对象 用来注入到  shiro的   securityManager 属性  cacheManager 中

 <!--spring rediscache-->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"
c:redisOperations-ref="redisTemplate">
<!-- 默认缓存10分钟 -->
<property name="defaultExpiration" value="10"/>
<!-- key:prefix -->
<property name="usePrefix" value="true"/>
<!-- cacheName 缓存超时配置,半小时,一小时,一天 -->
<property name="expires">
<map key-type="java.lang.String" value-type="java.lang.Long">
<entry key="halfHour" value="1800"/>
<entry key="hour" value="3600"/>
<entry key="oneDay" value="86400"/>
<entry key="itzixiCaptcha" value="500"/>
<!-- shiro cache keys -->
<entry key="authenticationCache" value="1800"/><!-- 用户每次操作后会要等缓存过期后会重新再取 -->
<entry key="authorizationCache" value="1800"/><!-- 用户每次操作后会要等缓存过期后会重新再取 -->
<entry key="activeSessionCache" value="1800"/><!-- 用户session每次操作后会重置时间 -->
</map>
</property>
</bean> <!-- cache注解,项目中如果还存在shiro的ehcache的话,那么本文件和spring-ehcache.xml中的只能使用一个 -->
<cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/>

2、编写这两个 ShiroSpringCache.java

 import java.util.Collection;
import java.util.Collections;
import java.util.Set; import org.apache.shiro.cache.CacheException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper; /**
*
* @Title: ShiroSpringCache.java
* @Description: 实现spring cache作为shiro的缓存
* @date 2017年10月14日 下午12:08:56
* @version V1.0
*/
@SuppressWarnings("unchecked")
public class ShiroSpringCache<K, V> implements org.apache.shiro.cache.Cache<K, V> {
final static Logger logger = LoggerFactory.getLogger(ShiroSpringCache.class); private final org.springframework.cache.Cache cache; public ShiroSpringCache(Cache cache) {
if (cache == null) {
throw new IllegalArgumentException("Cache argument cannot be null.");
}
this.cache = cache;
} @Override
public V get(K key) throws CacheException {
if (logger.isTraceEnabled()) {
logger.trace("Getting object from cache [" + this.cache.getName() + "] for key [" + key + "]key type:" + key.getClass());
}
ValueWrapper valueWrapper = cache.get(key);
if (valueWrapper == null) {
if (logger.isTraceEnabled()) {
logger.trace("Element for [" + key + "] is null.");
}
return null;
}
return (V) valueWrapper.get();
} @Override
public V put(K key, V value) throws CacheException {
if (logger.isTraceEnabled()) {
logger.trace("Putting object in cache [" + this.cache.getName() + "] for key [" + key + "]key type:" + key.getClass());
}
V previous = get(key);
cache.put(key, value);
return previous;
} @Override
public V remove(K key) throws CacheException {
if (logger.isTraceEnabled()) {
logger.trace("Removing object from cache [" + this.cache.getName() + "] for key [" + key + "]key type:" + key.getClass());
}
V previous = get(key);
cache.evict(key);
return previous;
} @Override
public void clear() throws CacheException {
if (logger.isTraceEnabled()) {
logger.trace("Clearing all objects from cache [" + this.cache.getName() + "]");
}
cache.clear();
} @Override
public int size() {
return 0;
} @Override
public Set<K> keys() {
return Collections.emptySet();
} @Override
public Collection<V> values() {
return Collections.emptySet();
} @Override
public String toString() {
return "ShiroSpringCache [" + this.cache.getName() + "]";
}
}
ShiroSpringCacheManager.java
 import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.util.Destroyable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
*
* @Title: ShiroSpringCacheManager.java
* @Description: 实现shiro的cacheManager
* @date 2017年10月14日 下午12:09:49
* @version V1.0
*/
public class ShiroSpringCacheManager implements CacheManager, Destroyable { final static Logger logger = LoggerFactory.getLogger(ShiroSpringCacheManager.class); private org.springframework.cache.CacheManager cacheManager; public org.springframework.cache.CacheManager getCacheManager() {
return cacheManager;
} public void setCacheManager(org.springframework.cache.CacheManager cacheManager) {
this.cacheManager = cacheManager;
} @Override
public <K, V> Cache<K, V> getCache(String name) throws CacheException {
if (logger.isTraceEnabled()) {
logger.trace("Acquiring ShiroSpringCache instance named [" + name + "]");
}
org.springframework.cache.Cache cache = cacheManager.getCache(name);
return new ShiroSpringCache<K, V>(cache);
} @Override
public void destroy() throws Exception {
cacheManager = null;
} }

3、在applicationContext-shiro.xml配置文件中增加:

 <!-- 用户授权信息Cache, 采用spring-cache,  -->
<bean id="shiroRedisCacheManager" class="com.itzixi.web.shiro.ShiroSpringCacheManager">
<property name="cacheManager" ref="cacheManager"/>
</bean>

4、修改shiro安全管理器的使用对象如:

5、使用redis作为shiro的缓存注意,因为shiro的缓存是一个实体的对象,所有在序列化的时候必须使用jdk序列化

6、同时需要配置自定义Realm中开启缓存的配置,认证缓存、权限缓存、seesion缓存

7、在自定义Realm中 将配置的shiroReadisCacheManager对象作为参数传入到构造方法中注入进来

最新文章

  1. JS高级前端开发群加群说明及如何晋级
  2. java基础2_运算符,选择语句
  3. 彻底解决低端安卓手机touchend事件不触发(考虑scroll)
  4. thinkphp pathinfo nginx 无法加载模块:Index
  5. Cloudera5.8.3:Flume+Morphline+Solr开发小技巧
  6. JavaScript 输入内容就触发事件
  7. php intval()函数
  8. poj 1151 Atlantis (离散化 + 扫描线 + 线段树 矩形面积并)
  9. Web前端发展简史
  10. asp 下拉框二级联动
  11. react 编写组件 五
  12. 管理 sendmail 的邮件队列
  13. 解决Tomcat: Can&#39;t load IA 32-bit .dll on a AMD 64-bit platform 问题
  14. java解决hash算法冲突
  15. highcharts.js的时间轴折线图
  16. (21)The history of human emotions
  17. mac 终端光标在单词之间移动
  18. springmvc.xml 上传文件的配置
  19. Apache服务器SSL双向认证配置
  20. MySQL添加、删除索引

热门文章

  1. bzoj 4552 [Tjoi2016&amp;Heoi2016]排序——二分答案
  2. win7怎么安装和启动 jboss
  3. ASP.NET Ajax 客户端框架未能加载、&quot;Sys&quot;未定义
  4. Oracle合并某一列
  5. SpringBoot运行报Address already in use: bind
  6. ptyhon中文本挖掘精简版
  7. Cassandra Wiki Login JmxSecurity
  8. orzdba_monitor.sh和orzdba
  9. 使用wifi网卡笔记3---工具wpa_supplicant(STA模式)
  10. Centos7.4 版本环境下安装Mysql5.7操作记录