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管理可以提高效率。提高對資料的讀取的速度