shiro only provide the support of ehcache and concurrentHashMap. Here is an implement of redis cache can be used by shiro. Hope it will help you!
fork from https://github.com/alexxiyang/shiro-redis , add redis cluster support.
- use "git clone https://github.com/xchendeveloper/shiro-redis" to clone project to your local workspace and build jar file by your self
- download code direct
You can configure shiro-redis either in shiro.ini or in spring-*.xml
Here is the configuration for shiro.ini.
shiro.ini:
[main]
#====================================
# shiro-redis configuration [start]
#====================================
#===================================
# Redis Manager
#===================================
# Create redisManager
redisManager = org.crazycake.shiro.RedisManager
# Redis host. If you don't specify host the default value is 127.0.0.1 (Optional)
redisManager.host = 192.168.56.101:6379
# Redis cache key/value expire time. Default value: 3600 .The expire time is in second (Optional)
redisManager.expire = 600
# Redis connect timeout. Timeout for jedis try to connect to redis server(In milliseconds).(Optional)
redisManager.timeout = 0
# Redis password.(Optional)
#redisManager.password =
# Redis database. Default value is 0(Optional)
#redisManager.database = 0
#====================================
# Redis-based session configuration
#====================================
# Create redisSessionDAO
redisSessionDAO = org.crazycake.shiro.RedisSessionDAO
# Custom your redis key prefix for session management, if you doesn't define this parameter, shiro-redis will use 'shiro_redis_session:' as default prefix
# Note: Remember to add colon at the end of prefix.
redisSessionDAO.keyPrefix = shiro:session:
# Use redisManager as cache manager
redisSessionDAO.redisManager = $redisManager
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
sessionManager.sessionDAO = $redisSessionDAO
securityManager.sessionManager = $sessionManager
#=====================================
# Redis-based cache configuration
#=====================================
# Create cacheManager
cacheManager = org.crazycake.shiro.RedisCacheManager
# Custom your redis key prefix for cache management, if you doesn't define this parameter, shiro-redis will use 'shiro_redis_session:' as default prefix
# Note: Remember to add colon at the end of prefix.
cacheManager.keyPrefix = shiro:cache:
# Use redisManager as cache manager
cacheManager.redisManager = $redisManager
securityManager.cacheManager = $cacheManager
#=================================
# shiro-redis configuration [end]
#=================================If you use redis sentinel, config like this :
# Create redisManager
redisManager = org.crazycake.shiro.RedisSentinelManager
# Redis host. If you don't specify host the default value is 127.0.0.1 (Optional)
redisManager.host = 192.168.0.192:26379,192.168.0.192:26380,192.168.0.192:26381
# Redis cache key/value expire time. Default value:0 .The expire time is in second (Optional)
redisManager.expire = 600
# Redis connect timeout. Timeout for jedis try to connect to redis server(In milliseconds).(Optional)
redisManager.timeout = 2000
# timeout for jedis try to read data from redis server (Optional)
redisManager.soTimeout = 2000
# master name (Optional)
# redisManager.masterName = mymaster
# # Redis database. Default value is 0(Optional)
# redisManager.database = 0
# Redis password.(Optional)
# redisManager.password = chenxingIf you use redis cluster, config like this :
# Create redisManager
redisManager = org.crazycake.shiro.RedisClusterManager
# Redis host and port list
redisManager.host = 192.168.21.3:7000,192.168.21.3:7001,192.168.21.3:7002,192.168.21.3:7003,192.168.21.3:7004,192.168.21.3:7005
# Redis cache key/value expire time. Default value:0 .The expire time is in second (Optional)
redisManager.expire = 600
# Redis connect timeout. Timeout for jedis try to connect to redis server(In milliseconds).(Optional)
redisManager.timeout = 2000
# timeout for jedis try to read data from redis server (Optional)
redisManager.soTimeout = 2000
# max attempts to connect to server (Optional)
redisManager.maxAttempts = 2
# Redis password.(Optional)
#redisManager.password = xxxx
Here is a tutorial project for you to understand how to configure shiro-redis in shiro.ini.
spring.xml:
<!-- shiro-redis configuration [start] -->
<!-- shiro redisManager -->
<bean id="redisManager" class="org.crazycake.shiro.RedisManager">
<property name="host" value="127.0.0.1:6379"/>
<property name="expire" value="1800"/>
<!-- optional properties:
<property name="timeout" value="10000"/>
<property name="password" value="123456"/>
<property name="database" value="1"/>
-->
</bean>
<!-- Redis-based session configuration -->
<bean id="redisSessionDAO" class="org.crazycake.shiro.RedisSessionDAO">
<property name="redisManager" ref="redisManager" />
<property name="keyPrefix" value="shiro:session:" />
</bean>
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionDAO" ref="redisSessionDAO" />
</bean>
<!-- Redis-based cache configuration -->
<bean id="cacheManager" class="org.crazycake.shiro.RedisCacheManager">
<property name="redisManager" ref="redisManager" />
<property name="keyPrefix" value="shiro:cache:" />
</bean>
<!-- securityManager -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="sessionManager" ref="sessionManager" />
<property name="cacheManager" ref="cacheManager" />
<!-- other configurations -->
<property name="realm" ref="exampleRealm"/>
<property name="rememberMeManager.cipherKey" value="kPH+bIxk5D2deZiIxcaaaA==" />
</bean>
<!-- shiro-redis configuration [end] -->If you use redis sentinel, config like this :
<!-- shiro-redis configuration [start] -->
<!-- shiro redisManager -->
<bean id="redisManager" class="org.crazycake.shiro.RedisSentinelManager">
<property name="host" value="192.168.0.192:26379,192.168.0.192:26380,192.168.0.192:26381"/>
<property name="expire" value="1800"/>
<!-- optional properties:
<property name="timeout" value="10000"/>
<property name="soTimeout" value="10000"/>
<property name="masterName" value="mymaster"/>
<property name="password" value="123456"/>
<property name="database" value="1"/>
-->
</bean>If you use redis cluster, config like this :
<!-- shiro-redis configuration [start] -->
<!-- shiro redisManager -->
<bean id="redisManager" class="org.crazycake.shiro.RedisClusterManager">
<property name="host" value="192.168.21.3:7000,192.168.21.3:7001,192.168.21.3:7002,192.168.21.3:7003,192.168.21.3:7004,192.168.21.3:7005"/>
<property name="expire" value="1800"/>
<!-- optional properties:
<property name="timeout" value="10000"/>
<property name="soTimeout" value="10000"/>
<property name="maxAttempts" value="2"/>
<property name="password" value="123456"/>
-->
</bean>Here is a tutorial project for you to understand how to configure shiro-redis in spring configuration file.
Since redis only accept byte[], there comes to a serializer problem.
Shiro-redis is using StringSerializer as key serializer and ObjectSerializer as value serializer.
You can use your own custom serializer, as long as this custom serializer implemens org.crazycake.shiro.RedisSerializer
For example, you need to change the charset of keySerializer.
#=====================================
# Redis-based cache configuration
#=====================================
# Create cacheManager
cacheManager = org.crazycake.shiro.RedisCacheManager
# If you want change charset of keySerializer or use your own custom serializer, you need to define serializer first
cacheManagerKeySerializer = org.crazycake.shiro.StringSerializer
# Refer to https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html
# UTF-8, UTF-16, UTF-32, ISO-8859-1, GBK, Big5, etc
cacheManagerKeySerializer.charset = UTF-16
cacheManager.keySerializer = $cacheManagerKeySerializerThese 4 Serializers are replaceable:
- cacheManager.keySerializer
- cacheManager.valueSerializer
- redisSessionDAO.keySerializer
- redisSessionDAO.valueSerializer
Please send email to xchendevelop@163.com
可以用中文