Spring Boot 使用 Redis 进行 Session 共享

相关文章:

CentOS 1810 安装 Redis 5.0.3

Windows 中 安装 Redis 解压版

Windows 中 安装 Redis 可执行程序

Windows 中 安装 Redis 桌面连接工具(RedisDesktopManager)

在 IntelliJ IDEA 2018.2.5 创建 Maven 项目

Spring Boot 使用 Redis 进行 Session 共享(Ajax 跨域)

Spring Boot 使用 Redis 进行 Session 共享(子域使用 Cookie 共享 Session)


目录

项目配置:

创建项目一:

创建项目二:

Redis数据:

说明:


  1. 项目配置

    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

  2. 创建项目一:

    1. 点 Create New Project(创建新项目):

    2. 选择 Spring Initializr、Project SDK,点 Next:

    3. 配置项目(根据 项目配置 设置):

    4. 选择 Spring Boot 依赖:

      Spring Boot 版本:2.1.4
       

      模块

      模块中的依赖

      Core

      Session

      Web

      Web

      Thymeleaf Engines

      Thymeleaf

      NoSQL

      Redis

    5. 点 Enable Auto-Import:
    6. 删除 application.properties,新建 application.yml:
      推荐使用 .yml 配置文件。
    7. 在 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
    8. 在 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>
    9. 在包 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";
          }
          
      }
      
    10. 在包 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 {
      
      }
      
    11. 在 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 {
      
      }
      
    12. 运行项目,访问 http://127.0.0.1:10001,显示如下:

  3. 创建项目二:

    时间相同,代表session相同。 

    1. 项目配置等内容参照项目一

    2. 运行项目,访问 http://127.0.0.1:10002 ,显示如下:

  4. Redis数据:

  5. 说明:

    1. Redis Session 共享存在同一个浏览器中。

    2. 关闭浏览器后,重新访问会产生新的 Session。

    3. 此配置不支持跨域(访问两个项目时,需要使用相同的域名,如:127.0.0.1:10001、127.0.0.1:10002,其中127.0.0.1与localhost是两个域名)。

    4. 连接的是同一个Redis的同一个数据库(database)。

    5. 清空 Redis后重新访问,产生新的 Session。


项目源码下载地址:Spring Boot Redis Session 共享

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐晓伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值