1. 程式人生 > >解決使用redis進行基於shiro的session叢集共享,shiro+redis

解決使用redis進行基於shiro的session叢集共享,shiro+redis

說明:無需寫一行程式碼,直接配置就可以無縫銜接當前的shiro專案。

步驟一,pom配置:

<dependency>
			<groupId>org.crazycake</groupId>
			<artifactId>shiro-redis</artifactId>
			<version>2.4.2.1-RELEASE</version>
		</dependency>
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.7.2</version>
		</dependency>
步驟二,shiro的配置檔案的修改
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="userRealm" />
		<property name="sessionManager" ref="sessionManager" />
		<property name="cacheManager" ref="cacheManager" />
	</bean>


	<!-- shiro redisManager -->
	<bean id="redisManager" class="org.crazycake.shiro.RedisManager">
		<property name="host" value="127.0.0.1" />
		<property name="port" value="6379" />
		<property name="expire" value="1800" />
		<property name="timeout" value="180000" />
		<property name="password" value="111111" />
	</bean>

	<!-- redisSessionDAO -->
	<bean id="redisSessionDAO" class="org.crazycake.shiro.RedisSessionDAO">
		<property name="redisManager" ref="redisManager" />
	</bean>

	<!-- sessionManager -->
	<bean id="sessionManager"
		class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
		<property name="sessionDAO" ref="redisSessionDAO" />
	</bean>

	<!-- cacheManager -->
	<bean id="cacheManager" class="org.crazycake.shiro.RedisCacheManager">
		<property name="redisManager" ref="redisManager" />
	</bean>

	<bean id="userRealm" class="cn.com.****.system.web.filter.UserRealm">
		<property name="userAuthService" ref="userAuthService" />
		<!-- 關閉shiro的預設快取,自己解決 -->
		<property name="authenticationCachingEnabled" value="false" />
		<property name="authorizationCachingEnabled" value="false" />
	</bean>
	<bean id="userAuthService" class="cn.com.****.system.business.impl.UserBusImpl" />