色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

多個SpringBoot項目采用redis實現(xiàn)Session共享功能

瀏覽:68日期:2023-04-27 10:40:22

有時我們可能有多個不同的Web應(yīng)用,可以相互調(diào)用,這時如果每個應(yīng)用都有自己的session,那用戶跳轉(zhuǎn)到另一個應(yīng)用時就又需要登陸一次,這樣會帶來很不好的體驗,因此我們需要在不同的應(yīng)用中共享session。這里,我們采用redis來實現(xiàn)。

前置說明

由于只用到redis和springboot的整合,所以只能實現(xiàn)一個URL下的不同端口的應(yīng)用之間的session共享,如果連應(yīng)用名稱都完全不同的兩個應(yīng)用要實現(xiàn)session共享,在這個基礎(chǔ)上還需要使用到Nginx,這種方式我暫時還沒有試過。(SpringBoot項目默認(rèn)就是不帶應(yīng)用名稱的,除非自己在配置文件中修改過)

需要提前在本地安裝好redis,或者連接遠程redis服務(wù)器。這里就不寫安裝教程了,可以自行去網(wǎng)上搜索。

添加依賴

需要為springboot項目添加以下兩個依賴,參與session共享的項目都需要添加。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId></dependency>

一個是redis的依賴,一個是spring-session-data-redis的依賴。

配置redis參數(shù)

在SpringBoot項目的application.properties配置文件中配置redis參數(shù):

# Redis數(shù)據(jù)庫索引(默認(rèn)為0)spring.redis.database=0# Redis服務(wù)器地址,如果是遠程redis服務(wù)器,就改成服務(wù)器地址spring.redis.host=127.0.0.1# Redis服務(wù)器連接端口,默認(rèn)是6379spring.redis.port=6379# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)spring.redis.lettuce.pool.max-active=8# 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制)spring.redis.lettuce.pool.max-wait=-1ms# 連接池中的最大空閑連接spring.redis.lettuce.pool.max-idle=5# 連接池中的最小空閑連接spring.redis.lettuce.pool.min-idle=0# 連接超時時間(毫秒)spring.redis.timeout=5000spring.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過期時間

創(chuàng)建一個用于配置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'; } // 查找該用戶,成功后根據(jù)該用戶的類別返回到對應(yīng)頁面 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。

登錄過濾器

@Componentpublic 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對象,那么在存的時候,是會對該對象進行序列化的,取出時也會進行反序列化,因此該對象要實現(xiàn)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,不同應(yīng)用中的User對象的這個序列號必須相同,否則無法正確進行反序列化。

小結(jié)

之所以要實現(xiàn)這個功能是因為在我搭建自己的網(wǎng)站時想集成之前做過的另一個應(yīng)用,把它作為一個功能嵌入這個應(yīng)用中,通過http互通。中間遇到了很多坑,這種方式的主要缺點就是不能支持不同應(yīng)用名稱的應(yīng)用之間的session共享,下一次可以嘗試一下加入Nginx。

到此這篇關(guān)于多個SpringBoot項目采用redis實現(xiàn)Session共享功能的文章就介紹到這了,更多相關(guān)SpringBoot Session共享內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 亚洲视频网址 | 日韩欧美成人乱码一在线 | 九九视频免费观看 | 亚洲特一级毛片 | 91情国产l精品国产亚洲区 | 久久九九免费视频 | 欧美综合精品一区二区三区 | 欧美一级日本一级韩国一级 | 成人怡红院视频在线观看 | 久久久久久亚洲精品中文字幕 | 欧美精品首页 | www.黄色免费 | 成人免费夜片在线观看 | wwww.黄| 天天做天天爱夜夜大爽完整 | 久草免费福利视频 | 人与拘一级a毛片 | 日韩3级 | 国产成在线观看免费视频 | 欧美精品一区二区三区免费 | 正能量www正能量免费网站 | 91久久精品国产一区二区 | 欧美黄免在线播放 | α片毛片| 欧美人成毛片在线播放 | 91视频最新网站 | 黄人成a动漫片免费网站 | 一级做a爱视频 | 欧美日韩一区二区中文字幕视频 | 国内自拍网红在线综合 | 日本美女福利视频 | 香港全黄一级毛片在线播放 | 99热久久免费精品首页 | 久久久免费精品 | 美女张开腿让人桶 | 久久99热成人精品国产 | 亚洲欧洲日产国产 最新 | 久久久久久久久久综合情日本 | 日本高清一本二本三本如色坊 | 国产日产高清欧美一区二区三区 | 欧美一区二区三区在线观看 |