SpringBoot中的ThreadLocal保存請求用戶信息的實(shí)例demo
一、ThreadLocal概述
線程局部變量,創(chuàng)建一個線程變量后,針對這個變量可以讓每個線程擁有自己的變量副本,每個線程是訪問的自己的副本,與其他線程的相互獨(dú)立。
二、具體代碼demo實(shí)現(xiàn)
(1)創(chuàng)建user實(shí)例對象
@Data
public class UserDTO {
private Long userId;
private String UserName;
}(2)創(chuàng)建UserThreadLocal對象
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解析出用戶本處為了簡單,直接虛構(gòu)一個用戶
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) 注冊用戶攔截器
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new UserInterceptor());
}
}(5)編寫測試接口
@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保存請求用戶信息的文章就介紹到這了,更多相關(guān)SpringBoot ThreadLocal保存請求用戶信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot在filter中如何用threadlocal存放用戶身份信息
- springboot登錄攔截器+ThreadLocal實(shí)現(xiàn)用戶信息存儲的實(shí)例代碼
- SpringBoot ThreadLocal 簡單介紹及使用詳解
- SpringBoot+ThreadLocal+AbstractRoutingDataSource實(shí)現(xiàn)動態(tài)切換數(shù)據(jù)源
- Springboot公共字段填充及ThreadLocal模塊改進(jìn)方案
- SpringBoot ThreadLocal實(shí)現(xiàn)公共字段自動填充案例講解
- SpringBoot通過ThreadLocal實(shí)現(xiàn)登錄攔截詳解流程
- springboot 使用ThreadLocal的實(shí)例代碼
- SpringBoot中使用?ThreadLocal?進(jìn)行多線程上下文管理及注意事項(xiàng)小結(jié)
相關(guān)文章
Windows編寫jar啟動腳本和關(guān)閉腳本的操作方法
腳本文件,通常放入/bin目錄下,編寫啟動腳本需要保證能夠識別到對應(yīng)的jar文件,其次需要保證能夠識別到/config中的配置文件信息,這篇文章主要介紹了Windows編寫jar啟動腳本和關(guān)閉腳本的操作方法,需要的朋友可以參考下2022-12-12
java9新特性Reactive?Stream響應(yīng)式編程?API
這篇文章主要為大家介紹了java9新特性響應(yīng)式編程API的特點(diǎn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes
這篇文章主要介紹了springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
逆序?qū)栴}(Java實(shí)現(xiàn))歸并詳解
文章介紹了計(jì)算給定正整數(shù)序列中逆序?qū)?shù)量的方法,特別是使用歸并排序進(jìn)行拆分和合并時統(tǒng)計(jì)逆序?qū)?通過遞歸拆分序列,并在合并過程中統(tǒng)計(jì)逆序?qū)?shù)量,從而得到整個序列的逆序?qū)倲?shù),此方法能有效處理包含重復(fù)數(shù)字的序列2026-05-05
深入解析Java的Hibernate框架中的一對一關(guān)聯(lián)映射
這篇文章主要介紹了Java的Hibernate框架的一對一關(guān)聯(lián)映射,包括對一對一外聯(lián)映射的講解,需要的朋友可以參考下2016-01-01
使用Java 8中的Lambda表達(dá)式實(shí)現(xiàn)工廠模式
這篇文章主要給大家介紹了使用Java 8中的Lambda表達(dá)式實(shí)現(xiàn)工廠模式的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-04-04
Eclipse+Java+Swing+Mysql實(shí)現(xiàn)電影購票系統(tǒng)(詳細(xì)代碼)
這篇文章主要介紹了Eclipse+Java+Swing+Mysql實(shí)現(xiàn)電影購票系統(tǒng)并附詳細(xì)的代碼詳解,需要的小伙伴可以參考一下2022-01-01

