springboot学习(三)——spring session

本文介绍了Spring Session的使用,旨在将HTTP会话持久化并实现跨应用程序的会话共享。内容包括Session与Cookie的基础知识,Spring Session的配置,如在pom.xml中添加依赖,application.yml的配置,以及验证过程中发现的cookie中的sessionId与session.getId()不一致的问题,这可能是由于sessionId经过了Base64加密导致。
摘要由CSDN通过智能技术生成

前言

官网地址:https://docs.spring.io/spring-session/docs/current/reference/html5/guides/java-redis.html
目的:将session持久化,实现session共享,HttpSession的实现被Spring Session替换,操作HttpSession等同于操作redis中的数据。

1、session认识

1.1 Session 与cookie 基础

由于Http 协议是无状态协议,为了记住请求状态, 因此引入了Session和 Cookie 机制。 Session 存在于服务端, Cookie 存在于客户端。 维系客户端与服务端的桥梁, Session 相关的sessionId

1.2 关于HttpServletRequest,HttpSession

HttpSession是存在于HttpServletRequest的一个对象

当服务端往HttpSession中保存一些数据时,HttpServletRequest中自动添加了一个Cookie:JSESSIONID:xxxx,再后续的请求中, 浏览器也是自动的带上了这个Cookie,服务端根据Cookie中的JSESSIONID取到了对应的session。 服务端根据cookie中 的JSESSIONID来辨别身份。

2、spring session

2.1 pom.xml中添加依赖

  <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--pool-->
        
使用SpringBoot框架结合MyBatis实现Session共享和单点登录可以借助SpringSession和Redis来实现。 首先,需要配置SpringSession以使用Redis作为存储方式。可以在SpringBoot的配置文件中添加以下配置: ``` spring.session.store-type=redis spring.session.redis.namespace=spring:session spring.redis.host=127.0.0.1 spring.redis.port=6379 ``` 这样配置后,SpringSession会自动将session信息存储到Redis中。 接着,在登录验证成功后,将用户信息存储到Redis中,并将该用户的唯一标识存储到当前Session的属性中,以便后续验证是否登录。例如: ``` @RequestMapping("/login") public String login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) { // 验证用户名和密码 // ... // 验证通过后,将用户信息存储到Redis中,并设置Session属性 redisTemplate.opsForHash().put("user:" + username, "username", username); session.setAttribute("username", username); return "success"; } ``` 在后续的请求中,可以通过拦截器或过滤器来验证Session是否有效。例如: ``` @Component public class SessionInterceptor implements HandlerInterceptor { @Autowired private RedisTemplate<String, Object> redisTemplate; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); String username = (String) session.getAttribute("username"); if (StringUtils.isEmpty(username)) { response.sendRedirect("/login"); return false; } String storedUsername = (String) redisTemplate.opsForHash().get("user:" + username, "username"); if (!StringUtils.equals(storedUsername, username)) { response.sendRedirect("/login"); return false; } return true; } } ``` 以上代码片段展示了如何通过拦截器验证Session的有效性。首先从当前Session中获取用户名,如果为空则重定向到登录页面。然后从Redis中获取存储的用户名,如果与当前用户名不匹配,则重定向到登录页面。 这样就实现了SpringBoot、MyBatis、SpringSession和Redis共同完成Session共享和单点登录的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值