安装redis

$ wget http://download.redis.io/releases/redis-4.0.1.tar.gz
$ tar xzf redis-4.0.1.tar.gz
$ cd redis-4.0.1
$ make

配置redis.conf,允许外部连接

使用IntelliJ IDEA创建Spring Boot工程,选择web, redis, session.

LoginController.java

package com.example.springsessiondemo;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;@Controller
public class LoginController {@RequestMapping("/")@ResponseBodypublic String home(HttpServletRequest request, HttpServletResponse response) {HttpSession session = request.getSession();User user = (User) session.getAttribute("user");if (user == null){return "用好未登录" + session.getId();} else {return "用好已登录" + session.getId();}}@RequestMapping("/login")@ResponseBodypublic String login(HttpServletRequest request){User user = new User();user.setUsername("chf");user.setPassword("pass");request.getSession().setAttribute("user", user);return "登录成功 "  + request.getSession().getId();}
}

创建文件config.java

@Configuration
@ImportResource(locations={"classpath:spring.xml"})
public class Config {
}

配置文件application.yaml

server:port: 8000
spring:session:store-type: redisredis:host: 10.0.0.201port: 6379password:

配置文件spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer"><property name="domainName" value="example.com"/><property name="cookieName" value="JSESSIONID"/><property name="cookiePath" value="/" /><!-- <property name="domainNamePattern" value="^.+?\.(\w+\.[a-z]+)$"></property>--></bean><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>
</beans>

配置hosts文件

127.0.0.1 www.example.com
127.0.0.1 passport.example.com

运行程序,通过浏览器访问passport.example.com:8000

访问www.example.com:8000

JSESSIONID的内容相同,说明实现了会话共享。

接下来在passport进行登录

切换到www

一个简单的单点登录系统就实现了。

源代码工程

https://github.com/chenhaifeng2016/springsessiondemo

基于SpringBoot,SpringSession和redis的会话共享相关推荐

  1. 基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【八】【完善整个项目】

    上一章我们已经完成了菜单模块的开发工作,那么到了本章我们将完成我们角色管理模块的开发工作,在本章开始一个全新的模块进行开发的时候我们需要遵守一定的命名和开发规范如下: 1.我们的Controller的 ...

  2. spring-session用redis实现session共享实践

    什么是spring session? Spring Session provides an API and implementations for managing a user's session ...

  3. nginx+tomcat+redis实现会话共享

    1.环境准备(安装redis报错) nginx,两个tomcat,redis (因为测试全部安装在同一台) 安装redis: cd redis-4.0.9.tar.gz && cd r ...

  4. SpringBoot笔记:SpringBoot2.3集成SpringSession+nginx+redis实现session共享

    文章目录 Spring Session介绍 Redis集成 yml配置 依赖添加 redis存值查看 登录服务器查看redis的值 查询所有"spring:session:"开头的 ...

  5. 使用Shiro+Redis实现Session共享

    章节目录 1. 为什么要实现Session共享? 1.1 负载均衡 1.2 负载均衡中的Session问题 1.3 案例演示 2. Shiro架构 3. Shiro集成Redis 1. 为什么要实现S ...

  6. Tomcat通过Redis实现session共享的完整部署记录

    对于生产环境有了一定规模的tomcat集群业务来说,要实现session会话共享,比较稳妥的方式就是使用数据库持久化session.为什么要持久化session(共享session)呢?因为在客户端每 ...

  7. ubantu20.04服务器使用docker以及docker-compose编排部署前后端分离个人博客项目(springboot+vue+mysql+redis)

    ubantu20.04使用docker部署前后端分离个人博客项目(springboot+vue+mysql+redis) 前言 本博客是基于springboot+vue+jwt+redis+mysql ...

  8. C#session共享+redis_技术干货分享:基于SpringBoot+Redis的Session共享与单点登录

    categories: 架构 author: mrzhou tags: SpringBoot redis session 单点登录 基于SpringBoot+Redis的Session共享与单点登录 ...

  9. 在SpringBoot中使用Spring Session解决分布式会话共享问题

    在SpringBoot中使用Spring Session解决分布式会话共享问题 问题描述: 每次当重启服务器时,都会导致会员平台中已登录的用户掉线.这是因为每个用户的会话信息及状态都是由session ...

最新文章

  1. [OSDI 12] PoweGraph: 分布式图并行计算框架 学习总结
  2. 一颗椰子糖机器人_孩子编程启蒙机器人玩了不下10个,最推荐哪个呢?
  3. Interview:人工智能岗位面试—人工智能岗位求职之机器学习算法工程师必备知识框架结构图
  4. 数字42 它的C++二进制代码为 00101010
  5. linux内核那些事之mempolicy(1)
  6. 热门编程语言,热门开源项目,MS-DOS,Python,Calcurse等
  7. (95)Verilog HDL系统函数和任务:$fgets
  8. 表单防重复提交拦截器
  9. flex实现水平垂直居中
  10. Scrapy(一)爬知乎所有用户信息
  11. bootstrap 快速入门
  12. jQuery新浪微博表情插件教程
  13. HeadFirstJava 10数字与静态
  14. Unity3D学习(视频教程)
  15. 常见的弱口令字典1000~一石三鸟
  16. 如何下载网页上的音频
  17. SPSS 市场细分:客户画像\客户价值模型
  18. oracle 18c 新特性,Oracle 18c新特性测试
  19. 解决Visio和office365安装兼容问题
  20. 乐安全 支持x86_不用苦等五一 四款近期主打平板推荐

热门文章

  1. AJAX并不神秘:揭密各种AJAX控件和类库
  2. CodeForces - 946E Largest Beautiful Number(贪心+模拟)
  3. MATLAB优化问题
  4. 0459-Repeated Substring Pattern(重复的子字符串)
  5. 正态分布的峰度和偏度分别为_ML中的正态分布
  6. Chomp!博弈(巧克力游戏)
  7. HDU4514(非连通图的环判断与图中最长链)
  8. 外挂学习之路(8)--- 释放技能call
  9. C++设计模式之桥接模式
  10. 高级数据结构与算法 | 深度遍历搜索(DFS)与广度遍历搜索(BFS)