springboot使用Redis实现Session会话共享

文章讲述了如何在SpringBoot应用中使用Redis来实现Session的共享,包括在pom.xml添加依赖,配置application.yaml文件,以及创建测试用例展示Session的存取和失效操作。此外,还介绍了如何配置SpringBoot项目监听多个端口,包括在配置文件声明额外端口,编写配置类动态添加Connector,以及启动时应用该配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring boot中使用redis实现session的共享

目录结构:

1、在pom.xml中引入依赖

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-redis</artifactId>
	</dependency>
	<!-- spring2.X集成redis所需common-pool2-->
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-pool2</artifactId>
	</dependency>

2,创建并配置application.yaml文件

3,测试类SessionTestCtrl
此类有3个方法:
3.1,login(): 模拟登陆,在session中存储一个值

  @RequestMapping("/login")
    public Map<String,Object> login(HttpServletRequest request) {
        HttpSession httpSession = request.getSession();
        // 设置session中的值
        httpSession.setAttribute("username", "hry" + System.currentTimeMillis());
        Map<String,Object> rtnMap = new HashMap&l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值