1. 程式人生 > >Spring Session實現Session管理的原理與詳細配置

Spring Session實現Session管理的原理與詳細配置

原理請參考這篇文章很詳細:點選檢視

以下是我的在開發中使用配置

如果是maven工程,加入如下依賴:

                <!-- spring-session -->
		<dependency>
		  <groupId>org.springframework.session</groupId>
		  <artifactId>spring-session-data-redis</artifactId>
		  <version>1.2.1.RELEASE</version>
		</dependency>
		<!-- jedis -->
		<dependency>
		  <groupId>redis.clients</groupId>
		  <artifactId>jedis</artifactId>
		  <version>2.8.1</version>
		</dependency>


在spring的配置檔案中加入如下:

    <bean id="redisHttpSessionConfiguration"
	      class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
	    <property name="maxInactiveIntervalInSeconds" value="600"/>
	</bean>
	
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
	    <property name="maxTotal" value="100" />
	    <property name="maxIdle" value="10" />
	</bean>
	
	<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
	    <property name="hostName" value="redis服務地址"/>
	    <property name="port" value="6379"/>
	    <property name="password" value="密碼" />
	    <property name="timeout" value="3000"/>
	    <property name="usePool" value="true"/>
	    <property name="poolConfig" ref="jedisPoolConfig"/>
	</bean>
然後在web.xml檔案中加入他的攔截器:
	<!-- 載入spring容器 -->
 	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:context.xml</param-value>
	</context-param>
	<listener>
  		<listener-class>
  			 org.springframework.web.context.ContextLoaderListener
  		</listener-class>
 	</listener>
 	
	<!-- 配置spring-session攔截器 -->
        <filter>
	    <filter-name>springSessionRepositoryFilter</filter-name>
	    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	</filter>
        <filter-mapping>
	    <filter-name>springSessionRepositoryFilter</filter-name>
	    <url-pattern>/*</url-pattern>
	</filter-mapping>