Shiro Cache to Redis management
tags (space delimited): Shiro Redis
Objective:
The following methods are only used by single-use Redis
I. Importing jars for Shiro and Redis
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
Second, the concrete realization
Jedisutils Tool Class
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 (+);
Jedisconfig.setmaxidle (ten);
Jedisconfig.setmaxwaitmillis (+);
Host name and port number, open Redis server and port number
Jedispool = new Jedispool (jedisconfig, "192.168.0.118", 6379);
}
public static Jedis Getjedis () {
return Jedispool.getresource ();
}
public static void Close (Jedis Jedis) {
jedis.close ();
}
}
Handing Shiro cache to Redis requires implementation of the Shiro caching interface
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; /** * Custom 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 {byt
e[] bs = serializationutils.serialize ((Serializable) key);
byte[] Value = Jedisutils.getjedis (). get (BS);
if (value = = null) {return null;
} return Serializationutils.deserialize (value);
/** * Save the Shiro cache to Redis */public object put (object key, Object value) throws Cacheexception { Jedis Jedis = Jedisutils.getjedis(); Serialization and deserialization of Jedis.set (Serializationutils.serialize ((Serializable) key), Serializationutils.serialize ((Serializa
ble));
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);
}/** * Clears all caches */public void Clear () throws Cacheexception {Jedisutils.getjedis (). FLUSHDB ();
}/** * Number of caches */public int size () {Long size = Jedisutils.getjedis (). Dbsize ();
return Size.intvalue (); }/** * Get all keys */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;
}/** * Gets all the 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; }
}
Custom Customcachemanager Implementation 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> ();
}
}
Configured in the Shiro configuration, 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://WW
W.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.springframewo Rk.org/schema/context/spring-context-3.2.xsd/HTTPWww.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "> <!--creating Shirofilterfactorybean--<bean id= "Shirofilterfactorybean" class= "Org.apache.shiro . Spring.web.ShiroFilterFactoryBean "> <!--Dependency Injection security Manager-<property name=" SecurityManager "ref=" security Manager "/> <!--Set Default login page--<property name=" loginurl "value="/xxx/login.jsp "/> <!--user access not to Its authorized resources when the connection is displayed--<property name= "Unauthorizedurl" value= "/unauthorizedanderror.jsp"/> <!--specify resource access rights --<property name= "filterchaindefinitions" > <value>/** = authc & Lt;/value> </property> </bean> <!--Create a security Manager--<bean id= "SecurityManager" class= "Org.apache. Shiro.web.mgt.DefaultWebSecurityManager "> <!--dependent Custom realms-<property name=" Realm ref= "Customerrea LM "/> <!--dependent slowSave-<property name= "CacheManager" ref= "CacheManager"/> </bean> <!--Create custom realm--<bean I D= "Customerrealm" class= "Com.baizhi.shiro.realm.CustomerRealm" > <!--dependent voucher match--<property name= "Crede Ntialsmatcher "ref=" Credentialsmatcher "/> <!--turn on authentication and authorization--" The authentication cache here cannot be turned on for unknown reason. "<!--<property name=" authenticationcachingenabled "value=" true "/> <property name=" Authenticationcach Ename "value=" Authentication "/>--<property name=" authorizationcachingenabled "value=" true "/> <pr Operty name= "Authorizationcachename" value= "Authorization"/> </bean> <!--Create voucher match--<bean id= " Credentialsmatcher "class=" Org.apache.shiro.authc.credential.HashedCredentialsMatcher "> <property name=" Hashalgorithmname "value=" MD5 "/> <property name=" hashiterations "value=" 1024x768 "/> </bean> <!--open permission annotations Function--<bean class= "Org.apache.shiro.spring.security.interCeptor. Authorizationattributesourceadvisor "> <!--rely on security Manager--<property name=" SecurityManager "ref=" Securitym Anager "/> </bean> <!--Create Cache Manager--Custom Customcachemanager <!--redis Cache--<bean id=" Cach
Emanager "class=" Com.xxx.cache.shiro.CustomCacheManager "/>
Summarize
Giving the Shiro cache to Redis management can improve efficiency. Increase the speed of reading data