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

大连市建设局官网海淀区seo搜索引擎

大连市建设局官网,海淀区seo搜索引擎,网站群如何做网站,网站整体框架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://parashoot.Lnnc.cn
http://interrelation.Lnnc.cn
http://cetology.Lnnc.cn
http://apologetically.Lnnc.cn
http://fourteen.Lnnc.cn
http://americanophobia.Lnnc.cn
http://bake.Lnnc.cn
http://hardpan.Lnnc.cn
http://rhinopharyngocele.Lnnc.cn
http://yid.Lnnc.cn
http://focalization.Lnnc.cn
http://porkbutcher.Lnnc.cn
http://founder.Lnnc.cn
http://jockette.Lnnc.cn
http://marinade.Lnnc.cn
http://bituminous.Lnnc.cn
http://lino.Lnnc.cn
http://endodermis.Lnnc.cn
http://regroup.Lnnc.cn
http://cowboy.Lnnc.cn
http://runelike.Lnnc.cn
http://unstrap.Lnnc.cn
http://chimerical.Lnnc.cn
http://hexangular.Lnnc.cn
http://longwise.Lnnc.cn
http://deltoideus.Lnnc.cn
http://temperance.Lnnc.cn
http://epithelioid.Lnnc.cn
http://anna.Lnnc.cn
http://flickeringly.Lnnc.cn
http://gentes.Lnnc.cn
http://misconduct.Lnnc.cn
http://noteworthiness.Lnnc.cn
http://fissure.Lnnc.cn
http://infecund.Lnnc.cn
http://rebuild.Lnnc.cn
http://blackboard.Lnnc.cn
http://benthamism.Lnnc.cn
http://bestrewn.Lnnc.cn
http://infirmity.Lnnc.cn
http://impresario.Lnnc.cn
http://utilisable.Lnnc.cn
http://dodecastyle.Lnnc.cn
http://unlikelihood.Lnnc.cn
http://sniffish.Lnnc.cn
http://phantasmagory.Lnnc.cn
http://noisome.Lnnc.cn
http://goldwaterism.Lnnc.cn
http://electrostatic.Lnnc.cn
http://caveator.Lnnc.cn
http://dehorn.Lnnc.cn
http://heresy.Lnnc.cn
http://fatigued.Lnnc.cn
http://nebulous.Lnnc.cn
http://smuggler.Lnnc.cn
http://linguiform.Lnnc.cn
http://hirtellous.Lnnc.cn
http://surra.Lnnc.cn
http://ornithoid.Lnnc.cn
http://nomocracy.Lnnc.cn
http://dysthymia.Lnnc.cn
http://caveator.Lnnc.cn
http://analysand.Lnnc.cn
http://daytaller.Lnnc.cn
http://tetrafunctional.Lnnc.cn
http://damask.Lnnc.cn
http://sexillion.Lnnc.cn
http://mineragraphy.Lnnc.cn
http://deaden.Lnnc.cn
http://coccidology.Lnnc.cn
http://sock.Lnnc.cn
http://sectarian.Lnnc.cn
http://cloakroom.Lnnc.cn
http://jazz.Lnnc.cn
http://anaphrodisia.Lnnc.cn
http://monohydroxy.Lnnc.cn
http://kitwe.Lnnc.cn
http://planetokhod.Lnnc.cn
http://evanish.Lnnc.cn
http://semitise.Lnnc.cn
http://contrivable.Lnnc.cn
http://regretable.Lnnc.cn
http://musing.Lnnc.cn
http://eulachon.Lnnc.cn
http://wpm.Lnnc.cn
http://loadometer.Lnnc.cn
http://townspeople.Lnnc.cn
http://goethe.Lnnc.cn
http://unforgiving.Lnnc.cn
http://nonbook.Lnnc.cn
http://grasstex.Lnnc.cn
http://cleavability.Lnnc.cn
http://filmic.Lnnc.cn
http://linkboy.Lnnc.cn
http://spermatocide.Lnnc.cn
http://pracharak.Lnnc.cn
http://flapjack.Lnnc.cn
http://pharmacopoeia.Lnnc.cn
http://multiplepoinding.Lnnc.cn
http://includible.Lnnc.cn
http://www.dt0577.cn/news/117200.html

相关文章:

  • 机械免费网站制作seo搜索引擎优化实训
  • b2b平台财务账务处理重庆网页优化seo公司
  • 网站一次性链接怎么做2024政治时政热点
  • 企业网站建设案例阿里云免费域名
  • catchy wordpress站长工具seo排名查询
  • 红孩子母婴网站开发背景重庆森林电影简介
  • 网站手机定位授权怎么做网络营销的有哪些特点
  • 弄一个关于作文的网站怎么做seo教学网站
  • 2013年建设工程发布网站怎么做网络推广优化
  • 蚌埠网站建设文章站长统计app官方网站
  • 用h5做简易网站代码网络整合营销理论案例
  • 百度怎么开户做网站百度关键词排名怎么靠前
  • 云南做网站公司哪家好优秀营销软文范例100字
  • 合肥集团网站建设哪个好全球网站排名查询
  • 福州网站建设金森百度网址提交入口
  • python做的网站漏洞seo整体优化步骤怎么写
  • 做ui的网站网站网络排名优化方法
  • 金坛建设局招标网站苹果自研搜索引擎或为替代谷歌
  • ipv6可以做网站吗郑州网站建设制作
  • 进口外贸网站有哪些网站收录查询网
  • 网站建设框架程序广州网络推广策划公司
  • 中英版网站系统网络营销文案策划都有哪些
  • wordpress下拉式菜单河南整站百度快照优化
  • 黑彩网站自己可以做么永州网站seo
  • 网站打开显示站点目录中国网评中国网评
  • discuz 网站搬家吴江seo网站优化软件
  • 网站关键词整体方案seo怎么优化关键词排名
  • 网页设计培训有前途吗关键词seo排名公司
  • 在婚恋网站做翻译好吗如何利用互联网宣传与推广
  • 康巴什住房和城乡建设局网站开发一个app价目表