为了账号安全,请及时绑定邮箱和手机立即绑定

Redis共享Session原理及示例

标签:
Java SpringBoot

Redis共享session的作用

  • 微服务自身可以保持无状态,应用实例数量的多少不会影响用户登录状态;
  • 可实现单点登录的踢出功能,如可以让上次异地登录的用户下线;
  • session在多个服务或服务器间共享,实现多站点单点登录(参考SSO原理)

Redis缓存session原理简述

其工作原理,可简单用图描述(假设服务A运行有有个多个实例):
图片描述

Springboot-session结合Redis示例

  • 添加maven依赖
<dependency>  
	<groupId>org.springframework.boot</groupId>  
	<artifactId>spring-boot-starter-redis</artifactId>
	<version>1.4.0.RELEASE</version>
</dependency>  
<dependency>  
	<groupId>org.springframework.session</groupId>  
	<artifactId>spring-session-data-redis</artifactId>  
</dependency>
<dependency>
	<groupId>org.springframework.session</groupId>
	<artifactId>spring-session</artifactId>
	<version>1.3.3.RELEASE</version>
</dependency>
  • 添加redis配置(application.properties)
spring.redis.database=1  
spring.redis.host=localhost
spring.redis.password=xxxxx
spring.redis.port=6379

# server.port=8080
server.port=8081
  • 添加配置类
package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration  
@EnableRedisHttpSession  
public class RedisSessionConfig {  
}
  • 添加controller
package com.example.demo.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RestController  
@RequestMapping(value = "/")
public class Hello {  
    @RequestMapping(value = "/set", method = RequestMethod.GET)  
    public Map<String, Object> firstResp (HttpServletRequest request){           
        request.getSession().setAttribute("testKey", "testValue");

        Map<String, Object> map = new HashMap<>(); 
        map.put("testKey", "testValue");  
        return map;
    }
  
    @RequestMapping(value = "/query", method = RequestMethod.GET)  
    public Object sessions (HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        map.put("sessionId", request.getSession().getId());
        map.put("testKey", request.getSession().getAttribute("testKey"));
        return map;
    }
} 
  • 浏览器访问测试

    • 启动两个springboot程序,分别监听8080,8081端口
    • 浏览器分别访问http://127.0.0.1:8080/set, 127.0.0.1:8081/query接口,效果如下:
      图片描述
    • 查看redis中的key
      图片描述

可以看出两个独立的应用已经共享了同一个session。

点击查看更多内容
4人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
241
获赞与收藏
202

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消