• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

多个SpringBoot项目采用redis实现Session共享功能

springboot 搞代码 4年前 (2022-01-05) 28次浏览 已收录 0个评论

这篇文章主要介绍了多个SpringBoot项目采用redis实现Session共享,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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

前置说明

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

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

添加依赖

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

  org.springframework.bootspring-boot-starter-data-redis org.springframework.sessionspring-session-data-redis

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

配置redis参数

在SpringBoot项目的application.properties配置文件中配置redis参数:

 # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址,如果是远程redis服务器,就改成服务器地址 spring.redis.host=127.0.0.1 # Redis服务器连接端口,默认是6379 spring.redis.port=6379 # 连接池最大连接数(使用负值表示没有限制) spring.redis.lettuce.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.lettuce.pool.max-wait=-1ms # 连接池中的最大空闲连接 spring.redis.lettuce.pool.max-idle=5 # 连接池中的最小空闲连接 spring.redis.lettuce.pool.min-idle=0 # 连接超时时间(毫秒) spring.re<em style="color:transparent">来源[email protected]搞@^&代*@码网</em>dis.timeout=5000 spring.session.store-type=redis

如果你的项目使用的是application.yml,就进行如下配置:

 spring: redis: database: 0 host: 127.0.0.1 port: 6379 lettuce: pool: max-idle: 8 min-idle: 0 max-active: 8 max-wait: -1ms timeout: 5000 session: store-type: redis

配置session过期时间

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

 import org.springframework.context.annotation.Configuration; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @Configuration @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30) public class SessionConfig { }

简单的登录逻辑

 @RequestMapping("/doLogin") public String doLogin(HttpServletRequest request, Model model){ String username = request.getParameter("username"); String password = request.getParameter("password"); if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){ model.addAttribute("errorMsg", "用户名和密码不能为空"); return "login"; } // 查找该用户,成功后根据该用户的类别返回到对应页面 User user = userService.getUserByUsernameAndPassword(username, password); if(user == null) { model.addAttribute("errorMsg", "用户名或密码错误"); return "login"; } else { request.getSession().setAttribute("currentUser", user); model.addAttribute("currentUser", user); String identity = user.getIdentity(); if("admin".equals(identity)){ return "admin"; }else{ return "user"; } } }

直接按照原来的方式将对象存入session:request.getSession().setAttribute("currentUser", user); 此时session会存入redis。

登录过滤器

 @Component public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); User currentUser = (User) session.getAttribute("currentUser"); if(currentUser == null){ response.sendRedirect(request.getContextPath() + "/toLogin"); return false; }else{ return true; } } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { } }

同样按原来的方式从session中取出对象:User currentUser = (User) session.getAttribute("currentUser"); 此时会从redis中取出该对象。

注意

如果只是存字符串等redis可以直接解析的对象,那就不会有什么问题,但是如果是存取对象就需要进行序列化了,比如上文中存的是我自定义的一个User对象,那么在存的时候,是会对该对象进行序列化的,取出时也会进行反序列化,因此该对象要实现Serializable接口,并且需要进行session共享的项目中都要有一个一模一样的对象,比如我的User定义如下:

 import java.io.Serializable; public class User implements Serializable { private String id; private String username; private String password; private String email; private String identity; private static final long serialVersionUID = -5809782578272943999L; // 省略getter、setter方法 }

注意这个序列号serialVersionUID,不同应用中的User对象的这个序列号必须相同,否则无法正确进行反序列化。

小结

之所以要实现这个功能是因为在我搭建自己的网站时想集成之前做过的另一个应用,把它作为一个功能嵌入这个应用中,通过http互通。中间遇到了很多坑,这种方式的主要缺点就是不能支持不同应用名称的应用之间的session共享,下一次可以尝试一下加入Nginx。

到此这篇关于多个SpringBoot项目采用redis实现Session共享功能的文章就介绍到这了,更多相关SpringBoot Session共享内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网

以上就是多个SpringBoot项目采用redis实现Session共享功能的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:多个SpringBoot项目采用redis实现Session共享功能

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址