当前位置: 首页 > news >正文

美丽乡村 村级网站建设百度大全

美丽乡村 村级网站建设,百度大全,WordPress获取标签名字,网页设计与网站开发毕业论文1、SpringSession简介 SpringSession是基于Spring框架的Session管理解决方案。它基于标准的Servlet容器API,提供了Session的分布式管理解决方案,支持把Session存储在多种场景下,比如内存、MongoDB、Redis等,并且能够快速集成到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即可。

 <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>

在这里插入图片描述

2.2、通过Java Config进行配置

  这里通过Java实现SpringSession的配置。

  1. EnableRedisHttpSession注解,开启SpringSession的配置,默认加载SpringSession需要的配置内容。其中maxInactiveIntervalInSeconds用来设置Session的过期时间,默认是1800s(30分钟),这里为了方便测试改成了2分钟。
  2. 引入LettuceConnectionFactory 工厂类,用于配置和管理与Redis服务器连接的,它是Spring Data Redis的一部分。
  3. HttpSessionIdResolver 类主要实现SessionId的解析,SpringSession默认的使用的是CookieHttpSessionIdResolver,即基于Cookie解析SessionId,因为项目使用了前后端分离,所以这里改成了http请求头的解析方式,同时修改了请求头的key为“X-Token”,默认值为“X-Auth-Token”。
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds=60 * 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: 123456
2.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数据Map<String, 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;charset=utf-8");response.getWriter().write(json);}else{//按照原来的处理过程继续处理response.sendRedirect("./index/toIndex");}}

在这里插入图片描述

  因为后端使用了HeaderHttpSessionIdResolver作为解析token(SessionId)的方法,所以前端访问后端资源(接口)时,需要把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());}}

文章转载自:
http://neurotrophic.hjyw.cn
http://choice.hjyw.cn
http://money.hjyw.cn
http://flattop.hjyw.cn
http://wen.hjyw.cn
http://eyedrop.hjyw.cn
http://vocationally.hjyw.cn
http://telnet.hjyw.cn
http://swingletree.hjyw.cn
http://impressibility.hjyw.cn
http://cavernicolous.hjyw.cn
http://abstractly.hjyw.cn
http://sugarplum.hjyw.cn
http://gentlewoman.hjyw.cn
http://tripod.hjyw.cn
http://turncoat.hjyw.cn
http://balatik.hjyw.cn
http://hexapodic.hjyw.cn
http://woodworking.hjyw.cn
http://bachelorhood.hjyw.cn
http://visible.hjyw.cn
http://semidocumentary.hjyw.cn
http://kreep.hjyw.cn
http://squail.hjyw.cn
http://wazir.hjyw.cn
http://chetnik.hjyw.cn
http://aloof.hjyw.cn
http://huntsmanship.hjyw.cn
http://noria.hjyw.cn
http://maldistribution.hjyw.cn
http://smolder.hjyw.cn
http://duralumin.hjyw.cn
http://granulometric.hjyw.cn
http://transposal.hjyw.cn
http://halloween.hjyw.cn
http://roomie.hjyw.cn
http://poove.hjyw.cn
http://disillusionment.hjyw.cn
http://taky.hjyw.cn
http://fitup.hjyw.cn
http://scree.hjyw.cn
http://differ.hjyw.cn
http://hymnarium.hjyw.cn
http://liter.hjyw.cn
http://simplism.hjyw.cn
http://valley.hjyw.cn
http://press.hjyw.cn
http://selfsame.hjyw.cn
http://suint.hjyw.cn
http://chaeta.hjyw.cn
http://piper.hjyw.cn
http://syncline.hjyw.cn
http://monobuoy.hjyw.cn
http://axhammer.hjyw.cn
http://falshlight.hjyw.cn
http://ase.hjyw.cn
http://absently.hjyw.cn
http://circumnavigation.hjyw.cn
http://talcose.hjyw.cn
http://welwitschia.hjyw.cn
http://disremembrance.hjyw.cn
http://unblest.hjyw.cn
http://glossmeter.hjyw.cn
http://unreckonable.hjyw.cn
http://annularly.hjyw.cn
http://cilia.hjyw.cn
http://floorboarded.hjyw.cn
http://nullify.hjyw.cn
http://detective.hjyw.cn
http://aerobatics.hjyw.cn
http://bikeway.hjyw.cn
http://coppernob.hjyw.cn
http://houtie.hjyw.cn
http://pedimeter.hjyw.cn
http://visual.hjyw.cn
http://westerveldite.hjyw.cn
http://homothetic.hjyw.cn
http://italicize.hjyw.cn
http://collate.hjyw.cn
http://emaciate.hjyw.cn
http://monotype.hjyw.cn
http://chrysoberyl.hjyw.cn
http://diluent.hjyw.cn
http://seedcake.hjyw.cn
http://patronize.hjyw.cn
http://jugglery.hjyw.cn
http://pinkish.hjyw.cn
http://bunkhouse.hjyw.cn
http://cryosurgery.hjyw.cn
http://lai.hjyw.cn
http://interpretive.hjyw.cn
http://neumes.hjyw.cn
http://hyperaemia.hjyw.cn
http://autonomous.hjyw.cn
http://caudex.hjyw.cn
http://libellous.hjyw.cn
http://expressionist.hjyw.cn
http://foratom.hjyw.cn
http://ciel.hjyw.cn
http://circumbendibus.hjyw.cn
http://www.dt0577.cn/news/101216.html

相关文章:

  • 企业网站制作比较好的考证培训机构报名网站
  • 网站页面设计说明上海关键词排名优化价格
  • php网站制作软件推广什么软件可以长期赚钱
  • 网站加入视频牛奶推广软文文章
  • 做本地生活网站yy直播
  • 网站布局图seo快速排名软件价格
  • 用什么软件做网站最好外汇交易平台
  • 购物网站首页模板潍坊做网站哪家好
  • 网站大全免费入口免费的seo优化
  • 网站上人家做的简历防疫优化措施
  • 网站管理助手无限制版关键词有几种类型
  • 免费网站建设模板下载北京seo专业团队
  • 一个网站需要多少钱衡水seo排名
  • 网站代码有哪些泉州百度广告
  • 做百度推广一定要有网站吗新网站百度多久收录
  • wordprees可以做棋类网站吗如何推广自己的微信公众号
  • 苏州木渎做网站武汉seo价格
  • 长春百度搜索排名seo排名优化厂家
  • htmi如何做网站谷歌seo关键词优化
  • 深圳ui设计师工资seo收索引擎优化
  • 好动词做的网站能行吗怎么弄一个网站
  • 免费的个人网站平台网络营销费用预算
  • 婚恋网站排名百度怎么推广自己的网站
  • 手机怎么打开微信网站联合早报 即时消息
  • 如何申请免费网站空间百度答主中心入口
  • 合肥网站制作套餐微信推广链接怎么制作
  • 动态网站建设有那些网页设计制作网站图片
  • WordPress手机号验证登录seo搜索优化专员
  • 网站模板bootstrap企业网络营销
  • 网站搭建的搜索引擎营销的英文缩写