开工开工,

准备条件:

1. 本地Redis,官网:http://redis.io/,windows下

https://github.com/ServiceStack/redis-windows

https://github.com/MSOpenTech/redis/releases msi版本

2. Maven下的项目

开始:

1. 依赖配置pom.xml

<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>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>

2. web.xml配置

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

3.SpringMVC.xml配置

<!-- spring session redis 配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"/>

<!--Redis 单机配置 -->
<!--
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1" />
<property name="port" value="6379" /> -->
<!--<property name="password" value="${redis.password}" />-->
<!--<property name="timeout" value="${redis.timeout}" />-->
<!--<property name="poolConfig" ref="jedisPoolConfig" /> -->
<!--<property name="usePool" value="true" /> -->
<!-- </bean> -->

<!-- Redis 集群配置 -->
<!--
<bean id="sentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<constructor-arg name="master" value="mymaster" />
<constructor-arg name="sentinelHostAndPorts">
<set>
<value>xxx.xx.x.xxx:xxxxx</value>
<value>xxx.xx.x.xxx:xxxxx</value>
<value>xxx.xx.x.xxx:xxxxx</value>
</set>
</constructor-arg>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg ref="sentinelConfig" />
</bean> -->

<!--
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean> -->

<!-- 将session放入redis 360分钟-->
<!--
<bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="21600" />
</bean> -->

就着完了,注意看集群配置,因为我是本地没有测试到集群。

这是第一种配置方式,第二种配置,上面的所有配置都不需要,除了pom的依赖文件。

第二种配置,添加两个类文件,什么都不用做了,ok.

@EnableRedisHttpSession
public class SessionConfig {

@Bean
public JedisConnectionFactory connectionFactory() {
JedisConnectionFactory connection = new JedisConnectionFactory();
connection.setPort(6379);
connection.setHostName("127.0.0.1");
return connection;
}
}

public class SessionInitializer extends AbstractHttpSessionApplicationInitializer
{
public SessionInitializer()
{
super(SessionConfig.class);
}
}

测试session,你打开你的网站,用浏览器,

打开 redis-cli.exe,执行 keys *,你就可以看到当前所有的keys值,

再打开一个浏览器,再执行 keys *,你会发现多了两个key,一个是记住过期的事件,

你可以打开 浏览器,F12,执行js new Date(value),把你的value贴进去,就知道是什么时候过期了。

参考引用:

使用spring-session把http session放到redis里面

Session集群 http://qtdebug.com/spring-security/5.%20Spring-Security-%E9%9B%86%E7%BE%A4.html

Redis配置:http://aiilive.blog.51cto.com/1925756/1627455

如何在Redis中查看Session数据?

(1)Http Session数据在Redis中是以Hash结构存储的。 
(2)可以看到,还有一个key="spring:session:expirations:1431577740000"的数据,是以Set结构保存的。这个值记录了所有session数据应该被删除的时间(即最新的一个session数据过期的时间)。

127.0.0.1:6379> keys *
1) "spring:session:expirations:1431577740000"
2) "spring:session:sessions:e2cef3ae-c8ea-4346-ba6b-9b3b26eee578"
127.0.0.1:6379> type spring:session:sessions:e2cef3ae-c8ea-4346-ba6b-9b3b26eee578
hash
127.0.0.1:6379> type spring:session:expirations:1431577740000
set
127.0.0.1:6379> keys *
1) "spring:session:expirations:1431527520000"
2) "spring:session:sessions:59f3987c-d1e4-44b3-a83a-32079942888b"
3) "spring:session:sessions:11a69da6-138b-42bc-9916-60ae78aa55aa"
4) "spring:session:sessions:0a51e2c2-4a3b-4986-a754-d886d8a5d42d"
5) "spring:session:expirations:1431527460000"

127.0.0.1:6379> hkeys spring:session:sessions:59f3987c-d1e4-44b3-a83a-32079942888b
1) "maxInactiveInterval"
2) "creationTime"
3) "lastAccessedTime"
4) "sessionAttr:attr1"

127.0.0.1:6379> hget spring:session:sessions:59f3987c-d1e4-44b3-a83a-32079942888b sessionAttr:attr1
"\xac\xed\x00\x05sr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x00\x03"

127.0.0.1:6379> hget spring:session:sessions:59f3987c-d1e4-44b3-a83a-32079942888b creationTime
"\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x01MM\x94(\xec"

Spring Session的更多相关文章

  1. spring session 和 spring security整合

    背景: 我要做的系统前面放置zuul. 使用自己公司提供的单点登录服务.后面的业务应用也是spring boot支撑的rest服务. 目标: 使用spring security管理权限包括权限.用户请 ...

  2. 基于Spring Boot/Spring Session/Redis的分布式Session共享解决方案

    分布式Web网站一般都会碰到集群session共享问题,之前也做过一些Spring3的项目,当时解决这个问题做过两种方案,一是利用nginx,session交给nginx控制,但是这个需要额外工作较多 ...

  3. Spring boot配合Spring session(redis)遇到的错误

    背景:本MUEAS项目,一开始的时候,是没有引入redis的,考虑到后期性能的问题而引入.之前没有引用redis的时候,用户登录是正常的.但是,在加入redis支持后,登录就出错!错误如下: . __ ...

  4. 使用Spring Session做分布式会话管理

    在Web项目开发中,会话管理是一个很重要的部分,用于存储与用户相关的数据.通常是由符合session规范的容器来负责存储管理,也就是一旦容器关闭,重启会导致会话失效.因此打造一个高可用性的系统,必须将 ...

  5. Spring Session - Spring Boot

    The completed guide can be found in the boot sample application. Updating Dependencies Before you us ...

  6. 单点登录实现(spring session+redis完成session共享)

    一.前言 项目中用到的SSO,使用开源框架cas做的.简单的了解了一下cas,并学习了一下 单点登录的原理,有兴趣的同学也可以学习一下,写个demo玩一玩. 二.工程结构 我模拟了 sso的客户端和s ...

  7. Spring Session实现分布式session的简单示例

    前面有用 tomcat-redis-session-manager来实现分布式session管理,但是它有一定的局限性,主要是跟tomcat绑定太紧了,这里改成用Spring Session来管理分布 ...

  8. 使用Spring Session实现Spring Boot水平扩展

    小编说:本文使用Spring Session实现了Spring Boot水平扩展,每个Spring Boot应用与其他水平扩展的Spring Boot一样,都能处理用户请求.如果宕机,Nginx会将请 ...

  9. XML配置spring session jdbc实现session共享

    概述 session的基础知识就不再多说. 通常,我们会把一个项目部署到多个tomcat上,通过nginx进行负载均衡,提高系统的并发性.此时,就会存在一个问题.假如用户第一次访问tomcat1,并登 ...

随机推荐

  1. Android反编译(二)之反编译XML资源文件

    Android反编译(二) 之反编译XML资源文件 [目录] 1.工具 2.反编译步骤 3.重新编译APK 4.实例 5.装X技巧 6.学习总结 1.工具 1).反编译工具  apktool http ...

  2. Windows Azure Cloud Service (40) 使用VS2013的publishSettings文件,发布Cloud Service

    <Windows Azure Platform 系列文章目录> 在之前的文档中,笔者已经介绍了如何使用本地证书上传至云端的方式,将本地的Cloud Service发布至云端. 在本章中,笔 ...

  3. Elasticsearch Javascript API增删改查

    查询 根据索引.类型.id进行查询: client.get({ index:'myindex', type:'mytype', id:1 },function(error, response){// ...

  4. Chrome开发者工具详解(4)-Profiles面板

    Chrome开发者工具详解(4)-Profiles面板 如果上篇中的Timeline面板所提供的信息不能满足你的要求,你可以使用Profiles面板,利用这个面板你可以追踪网页程序的内存泄漏问题,进一 ...

  5. SQL 语句的TOP,Distinct语句

    --Top获取前几条数据,一般都与Order By连用 SELECT TOP 3 * FROM dbo.MyStudent --查询Student表中前3条所有的数据 SELECT TOP 3 S_N ...

  6. ASP.NET MVC系列:为视图添加查询功能

    首先,在MoviesController里添加一个查询方法,代码如下 public ActionResult SearchIndex(string title) { //查询数据库中的电影表 var ...

  7. LINQ的Except方法

    在两个集合中,左边集合减去右边集合的元素: source code: List<int> a = new List<int>{ { }, { }, { } }; List< ...

  8. 初识ViewState

    ViewState用法与Session相似 ViewState不能跨页面传递值,与session相反,不占用服务器空间. ViewState在刷新后会失效. 防止刷新使ViewState回初始值,可以 ...

  9. jquery_layout

    http://layout.jquery-dev.com/documentation.cfm

  10. 【Java每日一题】20161104

    package Nov2016; public class Ques1104 { private String num; public void setNum(String num){ num = n ...