Skip to content

基于 Redis 的 Shrio 缓存,支持 Jedis 直连和 Spring-Data-Redis RedisTemplate。An implement of redis cache can be used by shiro, Support Jedis direct connect and Spring-Data-Redis RedisTemplate.

License

Notifications You must be signed in to change notification settings

ushelp/EasyShiro-Redis-Cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EasyShiro-Redis-Cache

基于 Redis 的 Shrio 缓存,支持 Jedis 直连和 Spring-Data-Redis RedisTemplate。

An implement of redis cache can be used by shiro, Support Jedis direct connect and Spring-Data-Redis RedisTemplate.

Thanks shiro-redis.

Featuter

  • 中文

    1. 支持基于 Jdeis 直接配置 Redis 连接( INI, XML

    2. 支持与 Spring 集成时,使用 Spring-Data-Redis 的 RedisTemplateXML

    3. 支持与 EasyShiro 集成

  • English

    1. Support direct configuration of Redis connection (INI, XML)

    2. Support for integration with Spring-Data-Redis RedisTemplate (XML)

    3. Support for integration EasyShiro

Maven

<dependency>
	<groupId>cn.easyproject</groupId>
	<artifactId>easyshiro-redis-cache</artifactId>
	<version>2.6.0-RELEASE</version>
</dependency>

Configure

RedisManager(Jedis)

  • shiro.ini

     #redisManager
     redisManager = cn.easyproject.shirorediscache.RedisManager
     #optional if you don't specify host the default value is 127.0.0.1
     redisManager.host = 127.0.0.1
     #optional , default value: 6379
     redisManager.port = 6379
     #optional, timeout for jedis try to connect to redis server(In milliseconds), not equals to expire time! 
     redisManager.timeout = 0
     #optional, password for redis server
     redisManager.password = 
     
     #============redisSessionDAO=============
     redisSessionDAO = cn.easyproject.shirorediscache.RedisSessionDAO
     redisSessionDAO.redisManager = $redisManager
     #optional, default value:0, never expire. The expire time is in second
     redisSessionDAO.expire = 1800
     
     sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
     
     sessionManager.sessionDAO = $redisSessionDAO
     securityManager.sessionManager = $sessionManager
     
     
     #============redisCacheManager===========
     cacheManager = cn.easyproject.shirorediscache.RedisCacheManager
     cacheManager.redisManager = $redisManager
     #custom your redis key prefix, if you doesn't define this parameter shiro-redis will use 'shiro_redis_session:' as default prefix
     cacheManager.keyPrefix = users:security:authz:
     securityManager.cacheManager = $cacheManager
  • shiro.xml

     <!-- shiro filter -->
     <bean id="ShiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
     	<property name="securityManager" ref="securityManager"/>
     	
     	<!--
     	<property name="loginUrl" value="/login.jsp"/>
     	<property name="successUrl" value="/home.jsp"/>  
     	<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
     	-->
     	<!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean  -->
     	<!-- defined will be automatically acquired and available via its beanName in chain        -->
     	<!-- definitions, but you can perform instance overrides or name aliases here if you like: -->
     	<!-- <property name="filters">
     		<util:map>
     			<entry key="anAlias" value-ref="someFilter"/>
     		</util:map>
     	</property> -->
     	<property name="filterChainDefinitions">
     		<value>
     			/login.jsp = anon
     			/user/** = anon
     			/register/** = anon
     			/unauthorized.jsp = anon
     			/css/** = anon
     			/js/** = anon
     			
     			/** = authc
     		</value>
     	</property>
     </bean>
     
     <!-- shiro securityManager -->
     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
     
     	<!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. -->
     	
     	<!-- sessionManager -->
     	<property name="sessionManager" ref="sessionManager" />
     	
     	<!-- cacheManager -->
     	<property name="cacheManager" ref="cacheManager" />
     	
     	<!-- By default the servlet container sessions will be used.  Uncomment this line
     		 to use shiro's native sessions (see the JavaDoc for more): -->
     	<!-- <property name="sessionMode" value="native"/> -->
     </bean>
     <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>	
     
     <!-- shiro redisManager -->
     <bean id="redisManager" class="cn.easyproject.shirorediscache.RedisManager">
     	<property name="host" value="127.0.0.1"/>
     	<property name="port" value="6379"/>
     	<!-- optional properties:
     	<property name="timeout" value="10000"/>
     	<property name="password" value="123456"/>
     	-->
     </bean>
     
     <!-- redisSessionDAO -->
     <bean id="redisSessionDAO" class="cn.easyproject.shirorediscache.RedisSessionDAO">
     	<property name="redisManager" ref="redisManager" />
     	<!-- optional, default value:0, never expire. The expire time is in second -->
     	<property name="expire" value="1800"/>
     </bean>
     
     <!-- sessionManager -->
     <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
     	<property name="sessionDAO" ref="redisSessionDAO" />
     </bean>
     
     <!-- cacheManager -->
     <bean id="cacheManager" class="cn.easyproject.shirorediscache.RedisCacheManager">
     	<property name="redisManager" ref="redisManager" />
     </bean>

Spring-Data-Redis RedisTemplate

  • shiro.xml

    RedisTemplate key is must StringRedisSerializer.

     <!-- RedisTemplate Start -->
     <!-- JedisPool -->
     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
     	<property name="maxTotal" value="${redis.pool.maxTotal}"></property>
     	<property name="maxIdle" value="${redis.pool.maxIdle}"></property>
     	<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"></property>
     	<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"></property>
     	<property name="testOnReturn" value="${redis.pool.testOnReturn}"></property>
     </bean>
     
     <bean id="jedisConnFactory"
     	class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
     	p:use-pool="true">
     	<property name="hostName" value="${redis.host}"></property>
     	<property name="port" value="${redis.port}"></property>
     	<property name="database" value="${redis.database}"></property>
     	<!-- <property name="password" value="pwd456"></property> -->
     	<property name="poolConfig" ref="jedisPoolConfig"></property>
     </bean>
     
     <!-- Redis template definition -->
     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
     	p:connection-factory-ref="jedisConnFactory">
     	
     	<!-- !IMPORTANT: key is must 'StringRedisSerializer' -->
     	<property name="keySerializer">
     		<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
     	</property>
     	<property name="hashKeySerializer">
     		<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
     	</property>
     	<property name="valueSerializer">
     		<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
     	</property>
     	<property name="hashValueSerializer">
     		<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
     	</property>
     </bean>
     <!-- RedisTemplate End-->
     
     	<!-- shiro filter -->
     <bean id="ShiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
     	<property name="securityManager" ref="securityManager"/>
     	
     	<!--
     	<property name="loginUrl" value="/login.jsp"/>
     	<property name="successUrl" value="/home.jsp"/>  
     	<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
     	-->
     	<!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean  -->
     	<!-- defined will be automatically acquired and available via its beanName in chain        -->
     	<!-- definitions, but you can perform instance overrides or name aliases here if you like: -->
     	<!-- <property name="filters">
     		<util:map>
     			<entry key="anAlias" value-ref="someFilter"/>
     		</util:map>
     	</property> -->
     	<property name="filterChainDefinitions">
     		<value>
     			/login.jsp = anon
     			/user/** = anon
     			/register/** = anon
     			/unauthorized.jsp = anon
     			/css/** = anon
     			/js/** = anon
     			
     			/** = authc
     		</value>
     	</property>
     </bean>
     
     <!-- shiro securityManager -->
     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
     
     	<!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. -->
     	
     	<!-- sessionManager -->
     	<property name="sessionManager" ref="sessionManager" />
     	
     	<!-- cacheManager -->
     	<property name="cacheManager" ref="cacheManager" />
     	
     	<!-- By default the servlet container sessions will be used.  Uncomment this line
     		 to use shiro's native sessions (see the JavaDoc for more): -->
     	<!-- <property name="sessionMode" value="native"/> -->
     </bean>
     <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>	
     
     <!-- redisSessionDAO -->
     <bean id="redisSessionDAO" class="cn.easyproject.shirorediscache.RedisSessionDAO">
     	<property name="redisTemplate" ref="redisTemplate" />
     	<!-- optional, default value:0, never expire. The expire time is in second -->
     	<property name="expire" value="1800"/>
     </bean>
     
     <!-- sessionManager -->
     <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
     	<property name="sessionDAO" ref="redisSessionDAO" />
     </bean>
     
     <!-- cacheManager -->
     <bean id="cacheManager" class="cn.easyproject.shirorediscache.RedisCacheManager">
     	<property name="redisTemplate" ref="redisTemplate" />
     </bean>

NOTE EasyShiro-Redis-Cache don't support SimpleAuthenticationInfo created by this constructor org.apache.shiro.authc.SimpleAuthenticationInfo.SimpleAuthenticationInfo(Object principal, Object hashedCredentials, ByteSource credentialsSalt, String realmName). Please use org.apache.shiro.authc.SimpleAuthenticationInfo.SimpleAuthenticationInfo(Object principal, Object hashedCredentials, String realmName) instead.

End

Email:inthinkcolor@gmail.com

http://www.easyproject.cn

Donation/捐助:


支付宝/微信/QQ/云闪付/PayPal 扫码支付
支付宝/微信/QQ/云闪付/PayPal

我们相信,每个人的点滴贡献,都将是推动产生更多、更好免费开源产品的一大步。

感谢慷慨捐助,以支持服务器运行和鼓励更多社区成员。

We believe that the contribution of each bit by bit, will be driven to produce more and better free and open source products a big step.

Thank you donation to support the server running and encourage more community members.

About

基于 Redis 的 Shrio 缓存,支持 Jedis 直连和 Spring-Data-Redis RedisTemplate。An implement of redis cache can be used by shiro, Support Jedis direct connect and Spring-Data-Redis RedisTemplate.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published