篇十九:session跨域

学习地址:http://www.cnblogs.com/beyang/p/6104802.html

简介:spring-session是spring的一个开源工程

源码地址:https://github.com/spring-projects/spring-session

官网地址:http://projects.spring.io/spring-session/

 

一、maven工程引入jar包

<!-- 自动回加载其它的依赖jar -->
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
    <version>1.3.0.RELEASE</version>
</dependency>

 

二、配置web.xml文件,添加拦截器重写 request.getSession()、HttpSession

  主要实现session存储至指定的redis缓存中,request.getSession()还是正常使用

<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>

 

三、springMVC-context.xml中添加依赖class

        <!-- spring-session共享配置redis连接 -->
        <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"></bean>
        <bean id="jedisConnectionFavtory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
            <property name="hostname" value="${redis.host}"></property>
            <property name="port" value="${redis.port}"></property>
            <property name="password" value="${redis.pass}"></property>
            <property name="timeout" value="${redis.timeout}"></property>
            <property name="poolConfig" ref="jedisPoolConfig"></property>
            <property name="usePool" value="true"></property>
        </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>

 

posted @ 2017-03-25 17:21  刘广平  阅读(349)  评论(0)    收藏  举报