SpringMVC集成Spring-Session进行分布式管理
                 jopen
                 10年前
            
                    项目使用的是SpringMVC+hibernate,听从老大的意见使用Spring-Session进行处理session,用redis托管Session。下面正式记录下处理方法。
1.如果项目之前没有整合过spring-data-redis的话,这一步需要先做,在maven中添加这几个依赖:
  
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> <version>1.0.1.RELEASE</version> </dependency> <dependency> <groupId>com.orange.redis-embedded</groupId> <artifactId>embedded-redis</artifactId> <version>0.6</version> </dependency>
  
  
  
<!-- redis -->  <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  </bean>     <bean id="jedisConnectionFactory"      class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">      <property name="hostName" value="${redis.host}" />      <property name="port" value="${redis.port}" />      <property name="password" value="${redis.pass}" />      <property name="timeout" value="${redis.timeout}" />      <property name="poolConfig" ref="jedisPoolConfig" />      <property name="usePool" value="true" />  </bean>     <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">      <property name="connectionFactory" ref="jedisConnectionFactory" />  </bean>     <!-- 将session放入redis -->  <bean id="redisHttpSessionConfiguration"  class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">      <property name="maxInactiveIntervalInSeconds" value="1800" />  </bean> 
  
  
  
<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>
  
  
#------------ redis ------------ redis.host=192.168.1.174 redis.port=6379 redis.pass= redis.timeout=1800
  
  
  
测试环节:
   1. 开启两个tomcat,eg:localhost:8888和localhot:8080 然后运行起来。
  
   2.开开一个浏览器(一定注意使用同一个浏览器),打开8080的一个tab,然后登录,进入个人中心(需要登录才能进入的界面)。登录之后,将连接拷贝到另外一个浏览器tab,端口改为8888,发现不用登录就已经登录进去了。
  
3.查看redis,Mac下可以使用rdm工具
    