wordpress建小说网站,自己做视频网站怎么让加载速度变快,专注移动网站建设,合肥中科大网站开发1、SpringSession简介 SpringSession是基于Spring框架的Session管理解决方案。它基于标准的Servlet容器API#xff0c;提供了Session的分布式管理解决方案#xff0c;支持把Session存储在多种场景下#xff0c;比如内存、MongoDB、Redis等#xff0c;并且能够快速集成到Spr…1、SpringSession简介 SpringSession是基于Spring框架的Session管理解决方案。它基于标准的Servlet容器API提供了Session的分布式管理解决方案支持把Session存储在多种场景下比如内存、MongoDB、Redis等并且能够快速集成到Spring应用程序中。使用SpringSession实现Session管理可以有效解决Session共享的问题提升系统的可伸缩性和可靠性。同时SpringSession还提供了一些扩展如Spring Session Data Redis、Spring Session JDBC等可用于与不同的数据源进行集成。 这边博客主要记录了如何在SpringBoot项目中整合SpringSession并基于Redis实现对Session的管理和事件监听具体过程如下
2、整合SpringSession的步骤
2.1、引用SpringSession相关依赖 这里引入了spring-session和Redis的相关依赖项目其他依赖根据自己的项目按需引入即可。其中spring-session依赖有很多版本根据Session存储场景区分这里我们引入spring-session-data-redis即可。 dependencygroupIdorg.springframework.session/groupIdartifactIdspring-session-data-redis/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId
/dependency2.2、通过Java Config进行配置 这里通过Java实现SpringSession的配置。
EnableRedisHttpSession注解开启SpringSession的配置默认加载SpringSession需要的配置内容。其中maxInactiveIntervalInSeconds用来设置Session的过期时间默认是1800s30分钟这里为了方便测试改成了2分钟。引入LettuceConnectionFactory 工厂类用于配置和管理与Redis服务器连接的它是Spring Data Redis的一部分。HttpSessionIdResolver 类主要实现SessionId的解析SpringSession默认的使用的是CookieHttpSessionIdResolver即基于Cookie解析SessionId因为项目使用了前后端分离所以这里改成了http请求头的解析方式同时修改了请求头的key为“X-Token”默认值为“X-Auth-Token”。
Configuration
EnableRedisHttpSession(maxInactiveIntervalInSeconds60 * 2)
public class QriverSpringSessionConfig {Beanpublic LettuceConnectionFactory connectionFactory(){return new LettuceConnectionFactory();}Beanpublic HttpSessionIdResolver sessionIdResolver() {return new HeaderHttpSessionIdResolver(X-Token);}}如果之前项目中没有引入Redis这里还需要增加Redis的相关链接信息如下所示
spring:redis:host: 127.0.0.1port: 6379ssl: falsedatabase: 0password: 1234562.3、前端获取token并作为鉴权标识 前端在登录系统成功时可以通过返回的response 的Headers中解析到Token值一般会在前端封装的http请求中进行全局处理如下下图所示: 同时也可以直接由后端作为响应结果进行返回如果使用这种方式需要后端配合进行token的返回因为项目里使用了SpringSecurity框架所以我这里直接在重写的AuthenticationSuccessHandler的onAuthenticationSuccess()方法中实现了代码如下
Overridepublic void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {boolean isAjax this.isAjaxRequest(request);if(isAjax){//ajax请求返回json数据MapString, Object map new HashMap();map.put(code, 0);map.put(msg, 用户登录成功);map.put(success, true);//map.put(user,authentication);String token request.getSession().getId();map.put(token,token);String json JSON.toJSONString(map);response.setContentType(text/json;charsetutf-8);response.getWriter().write(json);}else{//按照原来的处理过程继续处理response.sendRedirect(./index/toIndex);}}因为后端使用了HeaderHttpSessionIdResolver作为解析tokenSessionId的方法所以前端访问后端资源接口时需要把Token放到请求头中后台解析Token并校验鉴权。 至此当我们在请求需要鉴权后才能访问的资源时就会在Header上携带Token同时每次响应头中也会带有该Token值。也就算了完成了SpringSession的整合工作了。因为我们使用了SpringBoot来整合SpringSession很多工作都被SpringBoot自动配置完成了所以整个过程就会非常简单和方便了。而在Redis中Session数据的存储方式如下所示这里不再展开后续学习过程中再逐步记录。 3、Session生命周期事件监听 上述过程完成了SpringSession的整合如果我们想监听Session的创建和销毁事件我们可以通过监听SessionCreatedEvent和SessionDeletedEvent完成具体实现如下
3.1、通过EventListener注解实现
Component
public class QriverSessionEventListener {EventListenerpublic void handleSessionCreatedEvent(SessionCreatedEvent event) {// 可以执行创建事件的操作System.out.println(QriverSessionEventListener handleSessionCreatedEvent,Time: Calendar.getInstance().getTime());}EventListenerpublic void handleSessionDeletedEvent(SessionDeletedEvent event) {// 可以执行销毁事件的操作System.out.println(QriverSessionEventListener handleSessionDeletedEvent,Time: Calendar.getInstance().getTime());}
}3.2、通过实现HttpSessionListener接口实现
Component
public class QriverSessionListener implements HttpSessionListener {Overridepublic void sessionCreated(HttpSessionEvent event) {// 当新的Session创建时增加在线用户计数// 你可以在这里添加你的逻辑代码System.out.println(QriverSessionListener sessionCreated,Time: Calendar.getInstance().getTime());}Overridepublic void sessionDestroyed(HttpSessionEvent event) {// 当Session销毁时减少在线用户计数// 你可以在这里添加你的逻辑代码System.out.println(QriverSessionListener sessionCreated,sessionDestroyed: Calendar.getInstance().getTime());}}