基于SpringBoot,SpringSession和redis的会话共享
安装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的会话共享相关推荐
- 基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【八】【完善整个项目】
上一章我们已经完成了菜单模块的开发工作,那么到了本章我们将完成我们角色管理模块的开发工作,在本章开始一个全新的模块进行开发的时候我们需要遵守一定的命名和开发规范如下: 1.我们的Controller的 ...
- spring-session用redis实现session共享实践
什么是spring session? Spring Session provides an API and implementations for managing a user's session ...
- nginx+tomcat+redis实现会话共享
1.环境准备(安装redis报错) nginx,两个tomcat,redis (因为测试全部安装在同一台) 安装redis: cd redis-4.0.9.tar.gz && cd r ...
- SpringBoot笔记:SpringBoot2.3集成SpringSession+nginx+redis实现session共享
文章目录 Spring Session介绍 Redis集成 yml配置 依赖添加 redis存值查看 登录服务器查看redis的值 查询所有"spring:session:"开头的 ...
- 使用Shiro+Redis实现Session共享
章节目录 1. 为什么要实现Session共享? 1.1 负载均衡 1.2 负载均衡中的Session问题 1.3 案例演示 2. Shiro架构 3. Shiro集成Redis 1. 为什么要实现S ...
- Tomcat通过Redis实现session共享的完整部署记录
对于生产环境有了一定规模的tomcat集群业务来说,要实现session会话共享,比较稳妥的方式就是使用数据库持久化session.为什么要持久化session(共享session)呢?因为在客户端每 ...
- ubantu20.04服务器使用docker以及docker-compose编排部署前后端分离个人博客项目(springboot+vue+mysql+redis)
ubantu20.04使用docker部署前后端分离个人博客项目(springboot+vue+mysql+redis) 前言 本博客是基于springboot+vue+jwt+redis+mysql ...
- C#session共享+redis_技术干货分享:基于SpringBoot+Redis的Session共享与单点登录
categories: 架构 author: mrzhou tags: SpringBoot redis session 单点登录 基于SpringBoot+Redis的Session共享与单点登录 ...
- 在SpringBoot中使用Spring Session解决分布式会话共享问题
在SpringBoot中使用Spring Session解决分布式会话共享问题 问题描述: 每次当重启服务器时,都会导致会员平台中已登录的用户掉线.这是因为每个用户的会话信息及状态都是由session ...
最新文章
- RedLock: 看完这篇文章后请不要有任何疑惑了
- php ajax session失效,PHP中解决ajax请求session过期退出登录问题
- iOS架构-静态库.framework脚本化打包补充(5)
- 简单工厂模式(详细)(举例饮料)
- Thread类和Runable接口使用
- java中Date与String的相互转化
- 在不同的ObjectContext中更新数据
- GJM : Unity3D HIAR -【 快速入门 】 三、导入 SDK
- 20191006每日一句
- 单光感pid巡线_PID非常好的光感巡线思路
- 台式机连接校园网-UPC-lan-login
- ones,zeros,size用法总结
- Openssl win8 用法
- CF1108D Diverse Garland
- RSA组件之掩码生成函数(MGF)的实现(C源码)
- 别错过,教你如何用电脑玩手机
- C++中 #if #endif 和#ifdef #endif的用法
- 中乾山东最专业众筹系统
- 转载:提高脑力70法
- 方舟服务器金币系统,方舟指令快速获取金币方法大揭晓 你也可以变土豪
热门文章
- CodeForces - 560E Gerald and Giant Chess(组合数学+dp)
- 牛客 - Subset of Five(背包)
- CodeForces - 1359D Yet Another Yet Another Task(最大连续子段和)
- HDU - 3374 String Problem(最小表示法+最大表示法+KMP的next数组)
- UVA - 1606 Amphiphilic Carbon Molecules
- Markdown-基础语法
- [luoguP4705]玩游戏
- 22.循环控制.rs
- Mysql游标循环遍历
- 进程隐藏与进程保护(SSDT Hook 实现)(二)