从零开始的Spring Session(一) 中介绍了一些Session和Cookie的基础知识,这篇文章开始正式介绍Spring Session是如何对传统的Session进行改造的。官网这么介绍Spring Session:
Spring Session provides an API and implementations for managing a user’s session information. It also provides transparent integration with:web
HttpSession - allows replacing the HttpSession in an application container (i.e. Tomcat) neutral way. Additional features include:
Clustered Sessions - Spring Session makes it trivial to support clustered sessions without being tied to an application container specific solution.
Multiple Browser Sessions - Spring Session supports managing multiple users' sessions in a single browser instance (i.e. multiple authenticated accounts similar to Google).
RESTful APIs - Spring Session allows providing session ids in headers to work with RESTful APIs
WebSocket - provides the ability to keep the HttpSession alive when receiving WebSocket messages
其具体的特性很是之多,具体的内容能够从文档中了解到,笔者作一点本身的总结,Spring Session的特性包括但不限于如下:redis
使用GemFire来构建C/S架构的httpSession(不关注)
使用第三方仓储来实现集群session管理,也就是常说的分布式session容器,替换应用容器(如tomcat的session容器)。仓储的实现,Spring Session提供了三个实现(redis,mongodb,jdbc),其中redis使咱们最经常使用的。程序的实现,使用AOP技术,几乎能够作到透明化地替换。(核心)
能够很是方便的扩展Cookie和自定义Session相关的Listener,Filter。
能够很方便的与Spring Security集成,增长诸如findSessionsByUserName,rememberMe,限制同一个帐号能够同时在线的Session数(如设置成1,便可达到把前一次登陆顶掉的效果)等等
介绍完特性,下面开始一步步集成Spring Sessionspring
使用Redis集成Spring Sessionmongodb
引入依赖,Spring Boot的版本采用1.5.4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
配置
配置类开启Redis Http Sessionchrome
@Configuration
@EnableRedisHttpSession
public class HttpSessionConfig {
}
基本是0配置,只须要让主配置扫描到@EnableRedisHttpSession便可浏览器
配置文件application.yml,配置链接的redis信息tomcat
spring:
redis:
host: localhost
port: 6379
database: 0
编写测试Controller,以便于观察Spring Session的特性,和前一篇文章使用一样的代码
@Controller
public class CookieController {
@RequestMapping("/test/cookie")
public String cookie(@RequestParam("browser") String browser, HttpServletRequest request, HttpSession session) {
//取出session中的browser
Object sessionBrowser = session.getAttribute("browser");
if (sessionBrowser == null) {
System.out.println("不存在session,设置browser=" + browser);
session.setAttribute("browser", browser);
} else {
System.out.println("存在session,browser=" + sessionBrowser.toString());
}
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
System.out.println(cookie.getName() + " : " + cookie.getValue());
}
}
return "index";
}
}
启动类省略,下面开始测试。cookie
在浏览器中访问以下端点: http://localhost:8080/test/cookie?browser=chrome,下面是连续访问4次的结果session
1 不存在session,设置browser=chrome
2 存在session,browser=chrome
SESSION : 70791b17-83e1-42db-8894-73fbd2f2a159
3 存在session,browser=chrome
SESSION : 70791b17-83e1-42db-8894-73fbd2f2a159
4 存在session,browser=chrome
SESSION : 70791b17-83e1-42db-8894-73fbd2f2a159
若是还记得上一篇文章中运行结果的话,会发现和原生的session管理是有一些差异,原先的信息中咱们记得Cookie中记录的Key值是JSESSIONID,而替换成RedisHttpSession以后变成了SESSION。接着观察redis中的变化:数据结构
解析一下这个redis store,若是不纠结于细节,能够跳过,不影响使用。
总结
本节主要讲解了Spring Boot如何集成Spring Session,下一节将介绍更加复杂的特性。包括自定义Cookie序列化策略,与Spring Security的集成,根据用户名查找session等特性以及使用注意点。