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对象作为参数传入到构造方法中注入进来