springboot集成Spring Session
10.1 分布式集群环境下的集成(同域名、同项目)
10.1.1 创建SpringBoot的web支持项目07-springboot-session
创建项目
10.1.2 在pom.xml文件中添加依赖
<!-- 配置spring session的依赖 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<!-- 配置spring session data redis的依赖 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<!-- springboot集成redis的起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
10.1.3 在application.properties中配置端口号、上下文根及Redis连接
# 配置内嵌tomcat服务器信息
server.port=8080
server.servlet.context-path=/A07-springboot-session #配置redis连接信息
spring.redis.host=192.168.132.128
spring.redis.port=6379
spring.redis.password=123456 spring.session.store-type=redis
10.1.4 创建SessionController向session中存取数据
代码示例
package com.bjpowernode.springboot.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpSession; @RestController
public class SessionController { @GetMapping("/boot/set")
public String setSession(HttpSession session){
session.setAttribute("url","http://www.bjpowernode.com");
return "set session ok";
} @GetMapping("/boot/get")
public String getSession(HttpSession session){
String url= (String) session.getAttribute("url");
return url;
}
}
10.1.5 让项目使用SpringSession
- 在SpingBoot程序入口类Application上@EnableRedisHttpSession注解
package com.bjpowernode.springboot; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @SpringBootApplication
@EnableRedisHttpSession
public class A07SpringbootSessionApplication { public static void main(String[] args) {
SpringApplication.run(A07SpringbootSessionApplication.class, args);
} }
注解@EnableRedisHttpSession启用redis作为session容器
- 在application.properties配置文件中加入(可选):spring.session.store-type=redis
10.1.6 启动Redis服务

10.1.7 启动SpringBoot程序测试
设置地址:http://localhost:8080/A07-springboot-session/boot/set
获取地址:http://localhost:8080/A07-springboot-session/boot/get
- 通过Redis Desktop Manager查看Redis库中信息

- 修改服务器端口号,通过jar包的方式启动9090端口的tomcat,通过浏览器访问从session中取数据
springboot集成Spring Session的更多相关文章
- SpringBoot 集成 Spring Session
		SpringBoot 集成 Spring Session 应该讲解清楚,为什么要使用 Redis 进行 Session 的管理. Session 复制又是什么概念. Spring Session 在汪 ... 
- SpringBoot集成Spring Security(7)——认证流程
		文章目录 一.认证流程 二.多个请求共享认证信息 三.获取用户认证信息 在前面的六章中,介绍了 Spring Security 的基础使用,在继续深入向下的学习前,有必要理解清楚 Spring Sec ... 
- SpringBoot集成Spring Security(6)——登录管理
		文章目录 一.自定义认证成功.失败处理 1.1 CustomAuthenticationSuccessHandler 1.2 CustomAuthenticationFailureHandler 1. ... 
- SpringBoot集成Spring Security(4)——自定义表单登录
		通过前面三篇文章,你应该大致了解了 Spring Security 的流程.你应该发现了,真正的 login 请求是由 Spring Security 帮我们处理的,那么我们如何实现自定义表单登录呢, ... 
- Springboot集成Spring Batch
		Spring官网 (https://spring.io/projects/spring-batch#overview)对Spring Batch的解释: 一个轻量级的.全面的批处理框架,用于开发对企 ... 
- SpringBoot集成Spring Security入门体验
		一.前言 Spring Security 和 Apache Shiro 都是安全框架,为Java应用程序提供身份认证和授权. 二者区别 Spring Security:重量级安全框架 Apache S ... 
- SpringBoot集成Spring Security(5)——权限控制
		在第一篇中,我们说过,用户<–>角色<–>权限三层中,暂时不考虑权限,在这一篇,是时候把它完成了. 为了方便演示,这里的权限只是对角色赋予权限,也就是说同一个角色的用户,权限是 ... 
- SpringBoot集成Spring Security(2)——自动登录
		在上一章:SpringBoot集成Spring Security(1)——入门程序中,我们实现了入门程序,本篇为该程序加上自动登录的功能. 文章目录 一.修改login.html二.两种实现方式 2. ... 
- Spring boot集成spring session实现session共享
		最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么se ... 
随机推荐
- androidstudio出包问题--Warning: there were 1 unresolved references to classes or interfaces.
			问题:存在unresolved的类或接口导致打包失败 Warning: there were 1 unresolved references to classes or interfaces. You ... 
- iscsi-文件类型
			iSCSI简介(Internet SCSI): iSCSI 小型计算机系统接口,IBM公司研发,用于在IP网络上运行SCSI协议:解决了 SCSI需要直连存储设备的局限性:可以不停机扩展存储容量,iS ... 
- 安装nova
			在控制节点上执行 controllerHost='controller' controllerIP='172.31.240.49' MYSQL_PASSWD='m4r!adbOP' RABBIT_PA ... 
- C++进阶笔记
			思想原则: 以类为例,类最终要处理的是数据,方法只是过程,最终要改变的是private中的数据成员状态.程序设计也是如此,要的是数据. 一.const的作用 const定义变量:定义了一个不可修改的常 ... 
- JavaScript 3种内置对象
			前面我们学了对象,如何创建对象及使用对象. 内置对象不需要实例化,就可以使用. 可以通俗地理解,在内存里的东东是对象,也就是实例化好的.在磁盘里的东东是类,需要实例化才能使用.实例化后的东东在内存里. ... 
- 图表:WebChartControl
			#region 画统计图 /// <summary> /// 画统计图 /// </summary> private void LoadWebChartControl() { ... 
- Oracle通过正则表达式分割字符串 REGEXP_SUBSTR
			REGEXP_SUBSTR函数格式如下: function REGEXP_SUBSTR(string, pattern, position, occurrence, modifier) string ... 
- MySQL+navicat-1064 Error解决方案
			MySQL+navicat-1064 Error解决方案 错误 #1064 - You have an error in your SQL syntax; check the manual that ... 
- 编译错误ERROR C2027
			一个工程编译时出错! 费了很多时间,增加头文件都不可取,然后把source File文件下分的.cpp文件删除,然后编译通过. 
- 数位dp详解&&LG P2602 [ZJOI2010]数字计数
			数位dp,适用于解决一类求x~y之间有多少个符合要求的数或者其他. 例题 题目描述 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除 ... 
