SpringBoot中的ThreadLocal保存請(qǐng)求用戶信息的實(shí)例demo
一、ThreadLocal概述
線程局部變量,創(chuàng)建一個(gè)線程變量后,針對(duì)這個(gè)變量可以讓每個(gè)線程擁有自己的變量副本,每個(gè)線程是訪問的自己的副本,與其他線程的相互獨(dú)立。
二、具體代碼demo實(shí)現(xiàn)
(1)創(chuàng)建user實(shí)例對(duì)象
@Data public class UserDTO { private Long userId; private String UserName; }
(2)創(chuàng)建UserThreadLocal對(duì)象
public class UserThreadLocal { private UserThreadLocal(){}; private static final ThreadLocal<UserDTO> USER_DTO_THREAD_LOCAL = new ThreadLocal<>(); /** * 清除信息 */ public static void clear(){ USER_DTO_THREAD_LOCAL.remove(); } /** * 保存用戶信息 * @param userDTO */ public static void set(UserDTO userDTO){ USER_DTO_THREAD_LOCAL.set(userDTO); } public static UserDTO getCurrentUser(){ return USER_DTO_THREAD_LOCAL.get(); } }
(3)創(chuàng)建用戶攔截器
@Component public class UserInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //此處實(shí)際應(yīng)該根據(jù)header的token解析出用戶本處為了簡(jiǎn)單,直接虛構(gòu)一個(gè)用戶 UserDTO userDTo = new UserDTO(); userDTo.setUserId(10001L); userDTo.setUserName("張三"); UserThreadLocal.set(userDTo); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { UserThreadLocal.clear(); } }
(4) 注冊(cè)用戶攔截器
@Configuration public class InterceptorConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new UserInterceptor()); } }
(5)編寫測(cè)試接口
@RequestMapping("test") @RestController public class TestController { @GetMapping("get") public UserDTO getUser(){ UserDTO currentUser = UserThreadLocal.getCurrentUser(); System.out.println(currentUser); return currentUser; } }
(6)效果展示
到此這篇關(guān)于SpringBoot之ThreadLocal保存請(qǐng)求用戶信息的文章就介紹到這了,更多相關(guān)SpringBoot ThreadLocal保存請(qǐng)求用戶信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot在filter中如何用threadlocal存放用戶身份信息
- springboot登錄攔截器+ThreadLocal實(shí)現(xiàn)用戶信息存儲(chǔ)的實(shí)例代碼
- SpringBoot ThreadLocal 簡(jiǎn)單介紹及使用詳解
- SpringBoot+ThreadLocal+AbstractRoutingDataSource實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源
- Springboot公共字段填充及ThreadLocal模塊改進(jìn)方案
- SpringBoot ThreadLocal實(shí)現(xiàn)公共字段自動(dòng)填充案例講解
- SpringBoot通過ThreadLocal實(shí)現(xiàn)登錄攔截詳解流程
- springboot 使用ThreadLocal的實(shí)例代碼
- SpringBoot中使用?ThreadLocal?進(jìn)行多線程上下文管理及注意事項(xiàng)小結(jié)
相關(guān)文章
關(guān)于LinkedList集合對(duì)元素進(jìn)行增查刪操作
LinkedList集合內(nèi)部包含有兩個(gè)Node類型的first和last屬性維護(hù)一個(gè)雙向循環(huán)鏈表,在鏈表中的每一個(gè)元素都使用引用的方式來記住它的前一個(gè)元素和后一個(gè)元素,從而可以將所有的元素彼此連接起來,需要的朋友可以參考下2023-04-04java網(wǎng)絡(luò)通信技術(shù)之簡(jiǎn)單聊天小程序
這篇文章主要為大家詳細(xì)介紹了java網(wǎng)絡(luò)通信技術(shù)之簡(jiǎn)單聊天小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07SpringMvc框架的簡(jiǎn)介與執(zhí)行流程詳解
MVC是一種軟件設(shè)計(jì)典范,用一種業(yè)務(wù)邏輯、數(shù)據(jù)、界面顯示分離的方法組織代碼,將業(yè)務(wù)邏輯聚集到一個(gè)組件里面,在改進(jìn)和個(gè)性化定制界面及用戶交互的同時(shí),不需要重新編寫業(yè)務(wù)邏輯,MVC分層有助于管理和架構(gòu)復(fù)雜的應(yīng)用程序2021-06-06Java利用Phantomjs實(shí)現(xiàn)生成圖片的功能
這篇文章主要介紹了Java利用Phantomjs實(shí)現(xiàn)生成圖片的功能,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-08-08springboot前后臺(tái)數(shù)據(jù)交互的示例代碼
這篇文章主要介紹了springboot前后臺(tái)數(shù)據(jù)交互的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10