相关文章:
Windows 中 安装 Redis 桌面连接工具(RedisDesktopManager)
在 IntelliJ IDEA 2018.2.5 创建 Maven 项目
Spring Boot 使用 Redis 进行 Session 共享(Ajax 跨域)
Spring Boot 使用 Redis 进行 Session 共享(子域使用 Cookie 共享 Session)
目录
-
项目配置:
Group
Artifact
Name
Description
Package
端口
cn.com.xuxiaowei
demo1
demo1
Demo1 project for Spring Boot Redis Session 共享。
cn.com.xuxiaowei.demo1
10001
cn.com.xuxiaowei
demo2
demo2
Demo2 project for Spring Boot Redis Session 共享。
cn.com.xuxiaowei.demo2
10002
-
创建项目一:
-
点 Create New Project(创建新项目):
-
选择 Spring Initializr、Project SDK,点 Next:
-
配置项目(根据 项目配置 设置):
- 选择 Spring Boot 依赖:
Spring Boot 版本:2.1.4
模块
模块中的依赖
Core
Session
Web
Web
Thymeleaf Engines
Thymeleaf
NoSQL
Redis
- 点 Enable Auto-Import:
- 删除 application.properties,新建 application.yml:
推荐使用 .yml 配置文件。 - 在 application.yml 中添加:
# 服务器 配置 server: # 服务器端口 # 项目一(demo1)端口为10001 # 项目二(demo2)端口为10002 port: 10001 # Spring 配置 spring: # Thymeleaf 配置 thymeleaf: # 禁用 Thymeleaf 缓存 cache: false # Redis 配置 redis: # 数据库名称,默认为 0 # 连接工厂使用的数据库索引。 database: 1 # Redis 服务器主机。 host: 127.0.0.1 # Redis 服务器的登录密码。 password: # Redis 服务器端口。 port: 6379 # 日志配置 logging: level: web: debug
- 在 templates 文件夹中新建 index.html,内容为:
<!DOCTYPE html> <html lang="zh" xmlns:th="https://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Domo1-10001</title> <!--<title>Domo2-10002</title>--> </head> <body> <h2>项目名:Domo1</h2> <h2>端口号:10001</h2> <!--<h2>项目名:Domo2</h2>--> <!--<h2>端口号:10002</h2>--> [[${session.userInfo}]] </body> </html>
- 在包 cn.com.xuxiaowei.demo1 中创建包 controller,在 controller 中创建类 IndexController,内容如下:
package cn.com.xuxiaowei.demo1.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.time.LocalDateTime; /** * @author xuxiaowei */ @Controller public class IndexController { @RequestMapping("/") public String index(HttpServletRequest request, HttpServletResponse response, Model model) { HttpSession session = request.getSession(); Object userInfo = session.getAttribute("userInfo"); if (userInfo == null) { session.setAttribute("userInfo", LocalDateTime.now().toString()); }else { session.setAttribute("userInfo", userInfo); } return "/index"; } }
- 在包 cn.com.xuxiaowei.demo1 中创建包 config,在 config 中创建类 RedisCacheConfig,内容如下:
package cn.com.xuxiaowei.demo1.config; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Configuration; /** * Redis 开启声明缓存支持 * * @author xuxiaowei */ @Configuration @EnableCaching public class RedisCacheConfig { }
- 在 config 中创建类 RedisSessionConfig,内容如下:
package cn.com.xuxiaowei.demo1.config; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; /** * 开启 Redis Session 缓存 * * @author xuxiaowei */ @EnableRedisHttpSession public class RedisSessionConfig { }
- 运行项目,访问 http://127.0.0.1:10001,显示如下:
-
-
创建项目二:
时间相同,代表session相同。
-
项目配置等内容参照项目一。
-
运行项目,访问 http://127.0.0.1:10002 ,显示如下:
-
-
Redis数据:
-
说明:
-
Redis Session 共享存在同一个浏览器中。
-
关闭浏览器后,重新访问会产生新的 Session。
-
此配置不支持跨域(访问两个项目时,需要使用相同的域名,如:127.0.0.1:10001、127.0.0.1:10002,其中127.0.0.1与localhost是两个域名)。
-
连接的是同一个Redis的同一个数据库(database)。
-
清空 Redis后重新访问,产生新的 Session。
-
项目源码下载地址:Spring Boot Redis Session 共享