依赖
gradle compile "redis.clients:jedis:2.9.0" //spring-session compile('org.springframework.data:spring-data-redis:1.8.3.RELEASE') compile('org.springframework.session:spring-session:1.2.2.RELEASE')
web.xml 中配置
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-context.xml </param-value> </context-param> <!--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>
redis 配置 spring-session bean
<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate"> <property name="retryPolicy"> <bean class="org.springframework.retry.policy.TimeoutRetryPolicy"> <property name="timeout" value="50"/> <!--50毫秒后重试--> </bean> </property> </bean> <!-- jedis 连接池配置 --> <bean id="jedisSessionPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="300"/> <property name="maxIdle" value="5"/> <property name="minIdle" value="1"/> <property name="maxWaitMillis" value="1000"></property> <property name="testOnBorrow" value="true"/> <!-- 调用return 一个对象方法时,是否检查其有效性 --> <property name="testOnReturn" value="true"/> </bean> <bean id="redisSentConfSystemServer" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"> <property name="master"> <bean class="org.springframework.data.redis.connection.RedisNode"> <property name="name" value="${redis.sentinel.master.clound_data_server}"></property> </bean> </property> <property name="sentinels"> <set> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg index="0" value="${redis.sentinel1.address.clound_data_server}"/> <constructor-arg index="1" value="${redis.sentinel1.port.clound_data_server}"/> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg index="0" value="${redis.sentinel2.address.clound_data_server}"/> <constructor-arg index="1" value="${redis.sentinel2.port.clound_data_server}"/> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg index="0" value="${redis.sentinel3.address.clound_data_server}"/> <constructor-arg index="1" value="${redis.sentinel3.port.clound_data_server}"/> </bean> </set> </property> </bean> <bean id="jedisConnFactSystemServer01" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg ref="redisSentConfSystemServer"/> <property name="usePool" value="true"/> <property name="timeout" value="10000"/> <property name="database" value="${redis.db.clound_data_server}"/> <property name="password" value="${redis.password.clound_data_server}"/> <property name="poolConfig" ref="jedisSessionPoolConfig"/> </bean> <bean id="redisSessionTemplate01" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="jedisConnFactSystemServer01"/> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> </bean> <bean id="cacheUtil01" class="cn.mwee.utils.cache.RedisTemplateUtil"> <property name="stringRedisTemplate" ref="redisSessionTemplate01"/> <property name="retryTemplate" ref="retryTemplate"/> </bean> <!--redis config-end--> <!--spring-session--> <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="1800" /> <property name="cookieSerializer" ref="defaultCookieSerializer"/> </bean> <bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer"> <property name="domainName" value="${wpos.report.domainName}"/> <!--<property name="cookieName" value="JSESSIONID"/>--> <property name="cookiePath" value="${wpos.report.cookiePath}"></property> <!-- <property name="domainNamePattern" value="^.+?\.(\w+\.[a-z]+)$"></property>--> </bean>
遇到的问题
1:
web.xml 中配置 context-param 必须放在filter 前面
2 context-param 必须包含 redis spring-session bean 的配置
3 nginx 做轮询的时候 session 丢失了 造成 sessionid 每次都重新创建了
所以要配置 defaultCookieSerializer 这个bean
指定 domain 和 cookie path
关于这个问题 其实可以配置nginx 的时候 指定cook-path
解决nginx使用proxy_pass反向代理时,session丢失的问题 这2天在测试Nginx作为反向代理到Tomcat应用时,session丢失的问题。经过一系列查看官方文档和测试,发现如下: 1、如果只是host、端口转换,则session不会丢失。例如: location /testwx { proxy_pass http://127.0.0.1:8080/testwx; } 通过浏览器访问http://127.0.0.1/testwx时,浏览器的cookie内有jsessionid。再次访问时,浏览器会发送当前的cookie。 2、如果路径也变化了,则需要设置cookie的路径转换,nginx.conf的配置如下 location /testwx { proxy_pass http://127.0.0.1:8080/wx; } 通过浏览器访问http://127.0.0.1/testwx时,浏览器的cookie内没有jsessionid。再次访问时,后台当然无法获取到cookie了。 详细看了文档:http://nginx.org/en/docs/http/ngx_http_proxy_module.html?&_ga=1.161910972.1696054694.1422417685#proxy_cookie_path 加上路径转换:proxy_cookie_path /wx /testwx;则可以将wx的cookie输出到testwx上,Tomcat的session正常了。正确的配置是: location /testwx { proxy_pass http://127.0.0.1:8080/wx; proxy_cookie_path /wx /testwx;#这里的路径要注意对应关系 }
http://blog.csdn.net/eonianglutton/article/details/54139586
http://www.cnblogs.com/zangdalei/p/6021352.html