Shiro的缓存交给redis管理
标签(空格分隔):Shiro redis
前言:
以下方式只是单机版的redis使用
一、导入Shiro和redis的jar
jedis-2.7.3.jar
shiro-core-1.2.3.jar
shiro-ehcache-1.2.3.jar
shiro-spring-1.2.3.jar
shiro-web-1.2.3.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
二、具体实现
JedisUtils工具类
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisUtils { private static JedisPool jedisPool; static { JedisPoolConfig jedisConfig = new JedisPoolConfig(); jedisConfig.setMaxTotal(100); jedisConfig.setMaxIdle(10); jedisConfig.setMaxWaitMillis(100); //主机名称和端口号,开启redis的服务器和端口号 jedisPool = new JedisPool(jedisConfig, "192.168.0.118", 6379); } public static Jedis getJedis() { return jedisPool.getResource(); } public static void close(Jedis jedis) { jedis.close(); } }
将Shiro的缓存交给redis就需要实现Shiro的Cache接口
import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import org.apache.commons.lang3.SerializationUtils; import org.apache.shiro.cache.Cache; import org.apache.shiro.cache.CacheException; import redis.clients.jedis.Jedis; import com.baizhi.util.JedisUtils; /** * 自定义redis cache * @author MOTUI * */ public class ShiroRedisCache<K,V> implements Cache<K,V>{ // shiro cache key = value // redis key = value public Object get(Object key) throws CacheException { byte[] bs = SerializationUtils.serialize((Serializable)key); byte[] value = JedisUtils.getJedis().get(bs); if(value == null){ return null; } return SerializationUtils.deserialize(value); } /** * 将shiro的缓存保存到redis中 */ public Object put(Object key, Object value) throws CacheException { Jedis jedis = JedisUtils.getJedis(); //序列化 和 反序列化 jedis.set(SerializationUtils.serialize((Serializable)key), SerializationUtils.serialize((Serializable)value)); byte[] bs = jedis.get(SerializationUtils.serialize((Serializable)key)); Object object = SerializationUtils.deserialize(bs); return object; } public Object remove(Object key) throws CacheException { Jedis jedis = JedisUtils.getJedis(); byte[] bs = jedis.get(SerializationUtils.serialize((Serializable)key)); jedis.del(SerializationUtils.serialize((Serializable)key)); return SerializationUtils.deserialize(bs); } /** * 清空所有缓存 */ public void clear() throws CacheException { JedisUtils.getJedis().flushDB(); } /** * 缓存的个数 */ public int size() { Long size = JedisUtils.getJedis().dbSize(); return size.intValue(); } /** * 获取所有的key */ public Set keys() { Set<byte[]> keys = JedisUtils.getJedis().keys(new String("*").getBytes()); Set<Object> set = new HashSet<Object>(); for (byte[] bs : keys) { set.add(SerializationUtils.deserialize(bs)); } return set; } /** * 获取所有的value */ public Collection values() { Set keys = this.keys(); List<Object> values = new ArrayList<Object>(); for (Object object : keys) { byte[] bs = JedisUtils.getJedis().get(SerializationUtils.serialize((Serializable)object)); values.add(SerializationUtils.deserialize(bs)); } return values; } }
自定义CustomCacheManager实现CacheManager
import org.apache.shiro.cache.Cache; import org.apache.shiro.cache.CacheException; import org.apache.shiro.cache.CacheManager; public class CustomCacheManager implements CacheManager{ public <K, V> Cache<K, V> getCache(String name) throws CacheException { return new ShiroRedisCache<K,V>(); } }
在Shiro的配置中配置,shiro.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "> <!-- 创建shiroFilterFactoryBean --> <bean id="shiroFilterFactoryBean" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!-- 依赖注入安全管理器 --> <property name="securityManager" ref="securityManager"/> <!-- 设置默认的登录页面 --> <property name="loginUrl" value="/xxx/login.jsp"/> <!-- 用户访问未对其授权的资源时,所显示的连接 --> <property name="unauthorizedUrl" value="/unauthorizedAndError.jsp"/> <!-- 指定资源访问权限 --> <property name="filterChainDefinitions"> <value> /** = authc </value> </property> </bean> <!-- 创建安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!-- 依赖自定义realms --> <property name="realm" ref="customerRealm"/> <!-- 依赖缓存 --> <property name="cacheManager" ref="cacheManager"/> </bean> <!-- 创建自定义的realm --> <bean id="customerRealm" class="com.baizhi.shiro.realm.CustomerRealm"> <!-- 依赖凭证匹配器 --> <property name="credentialsMatcher" ref="credentialsMatcher"/> <!-- 开启认证和授权的缓存 --> 【此处的认证缓存不能开启,原因未知??】 <!-- <property name="authenticationCachingEnabled" value="true"/> <property name="authenticationCacheName" value="authentication"/> --> <property name="authorizationCachingEnabled" value="true"/> <property name="authorizationCacheName" value="authorization"/> </bean> <!-- 创建凭证匹配器 --> <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <property name="hashAlgorithmName" value="MD5"/> <property name="hashIterations" value="1024"/> </bean> <!-- 开启权限注解功能 --> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <!-- 依赖安全管理器 --> <property name="securityManager" ref="securityManager"/> </bean> <!-- 创建缓存管理器 --> 【自定义的CustomCacheManager】 <!-- redis缓存 --> <bean id="cacheManager" class="com.xxx.cache.shiro.CustomCacheManager"/>
总结
将Shiro的缓存交给redis管理可以提高效率。提高对数据的读取的速度