SpringBoot项目可以使用复述,数据库实现会话共享功能吗

  介绍

今天就跟大家聊聊有关SpringBoot项目可以使用复述,数据库实现会话共享功能吗,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

有时我们可能有多个不同的网络应用,可以相互调用,这时如果每个应用都有自己的会话,那用户跳转到另一个应用时就又需要登陆一次,这样会带来很不好的体验,因此我们需要在不同的应用中共享会话。这里,我们采用复述来实现。

由于只用到复述和SpringBoot的整合,所以只能实现一个URL下的不同端口的应用之间的会话共享,如果连应用名称都完全不同的两个应用要实现会话共享,在这个基础上还需要使用到Nginx,这种方式我暂时还没有试过。(SpringBoot项目默认就是不带应用名称的,除非自己在配置文件中修改过)

需要提前在本地安装好复述,或者连接远程复述,服务器。这里就不写安装教程了,可以自行去网上搜索。

需要为SpringBoot项目添加以下两个依赖,参与会话共享的项目都需要添加。

& lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-data-redis   & lt;/dependency>      & lt; dependency>   & lt; groupId> org.springframework.session   & lt; artifactId> spring-session-data-redis   & lt;/dependency>

一个是复述的依赖,一个是spring-session-data-redis的依赖。

在SpringBoot项目的的应用程序。属性配置文件中配置复述,参数:

 #复述,数据库索引(默认为0)
  spring.redis.database=0
  #复述,服务器地址,如果是远程复述,服务器,就改成服务器地址
  spring.redis.host=127.0.0.1
  #复述,服务器连接端口,默认是6379
  spring.redis.port=6379
  #连接池最大连接数(使用负值表示没有限制)
  spring.redis.lettuce.pool.max-active=8
  #连接池最大阻塞等待时间(使用负值表示没有限制)
  女士spring.redis.lettuce.pool.max-wait=1
  #连接池中的最大空闲连接
  spring.redis.lettuce.pool.max-idle=5
  #连接池中的最小空闲连接
  spring.redis.lettuce.pool.min-idle=0
  #连接超时时间(毫秒)
  spring.redis.timeout=5000
  spring.session.store-type=复述,

如果你的项目使用的是应用程序。yml、就进行如下配置:

春:   复述:   数据库:0   主持人:127.0.0.1   端口:6379   生菜:   池:   最大空闲:8   min-idle: 0   max-active: 8   最大等待:1女士   超时:5000   会话:   存储类型:复述,

创建一个用于配置会话过期时间的配置类:

进口org.springframework.context.annotation.Configuration;
  进口org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
  
  @ configuration
  @EnableRedisHttpSession (maxInactiveIntervalInSeconds=86400 * 30)
  公开课SessionConfig {
  }

 @RequestMapping (“/doLogin")
  公共字符串doLogin (HttpServletRequest请求,模型模型){
  用户名=request.getParameter字符串(“username");
  字符串密码=request.getParameter (“password");
  如果(StringUtils.isEmpty(用户名)| | StringUtils.isEmpty(密码)){
  model.addAttribute (“errorMsg",“用户名和密码不能为空“);
  返回“login";
  }//查找该用户,成功后根据该用户的类别返回到对应页面
  用户用户=userService。getUserByUsernameAndPassword(用户名、密码);
  如果(用户==null) {
  model.addAttribute (“errorMsg",“用户名或密码错误“);
  返回“login";
  其他}{
  request.getSession () .setAttribute (“currentUser"、用户);
  model.addAttribute (“currentUser"、用户);
  字符串标识=user.getIdentity ();
  如果“admin" .equals(身份)){
  返回“admin";
  其他}{
  返回“user";
  }
  }
  }

直接按照原来的方式将对象存入会话:<代码> request.getSession () .setAttribute (“currentUser"、用户);此时会话会存入复述。

@ component   公共类LoginInterceptor实现HandlerInterceptor {   @Override   公共布尔preHandle (HttpServletRequest请求,HttpServletResponse响应对象处理程序)   抛出异常{   HttpSession会话=request.getSession ();   用户currentUser=(用户)session.getAttribute (“currentUser");   如果(currentUser==null) {   response.sendRedirect (request.getContextPath () +“/toLogin");   返回错误;   其他}{   返回true;   }   }   @Override   公共空postHandle (HttpServletRequest HttpServletRequest HttpServletResponse HttpServletResponse,   对象o, ModelAndView ModelAndView)抛出异常{      }      @Override   公共空afterCompletion (HttpServletRequest HttpServletRequest HttpServletResponse HttpServletResponse,   对象o,异常e)抛出异常{      }   }

SpringBoot项目可以使用复述,数据库实现会话共享功能吗