当前位置:网站首页>ThreadLocal存储当前登录用户信息
ThreadLocal存储当前登录用户信息
2022-07-24 05:19:00 【青临emo】
ThreadLocal可以将用户信息保存在线程中,当请求结束后我们在把保存的信息清除掉。这样我们才开发的时候就可以直接从全局的ThreadLocal中很方便的获取用户信息
/** * 保存用户对象的ThreadLocal * * @author wangql */
@Component
public class UserInfoThreadHolder {
private static final ThreadLocal<SysUser> userThreadLocal = new ThreadLocal<>();
/** * 添加当前登录用户方法 */
public static void addCurrentUser() {
//登录对象从这里获取然后set进ThreadLocal
//具体根据业务逻辑来
SysUser sysUser = new SysUser();
userThreadLocal.set(sysUser);
}
public static SysUser getCurrentUser() {
return userThreadLocal.get();
}
/** * 防止内存泄漏 */
public static void remove() {
userThreadLocal.remove();
}
}
可以利用拦截器操作
/** * 将登录用户存入ThreadLocal * @author wangql */
@Component
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
UserInfoThreadHolder.addCurrentUser();
return true;
}
/** * 避免内存泄露 */
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
UserInfoThreadHolder.remove();
}
}
注册拦截器
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private LoginInterceptor loginInterceptor;
//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor).addPathPatterns("/**");
}
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.addExposedHeader("Authorization");
return corsConfiguration;
}
/** * 配置跨域 * @return */
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowCredentials(true)
.allowedMethods("GET", "POST", "DELETE", "PUT")
.maxAge(3600);
}
}
边栏推荐
- Collection = = academic waste
- 读《悟道:一位IT高管20年的职场心经》
- Vulnhub-Funbox: Rookie(Funbox2)靶机渗透
- 【mycat】mycat介绍
- How to forcibly uninstall Google browser? Don't worry about Google opening as a whiteboard. It's effective for personal testing.
- haclabs: no_name(HL.ova)靶机渗透-Vulnhub
- 开启Web3,曾经冷门的去中心化身份(DID)
- 【vsphere高可用】主机和虚拟机故障监测工作原理
- Cess test online line! The first decentralized storage network to provide multiple application scenarios
- 【activiti】流程实例
猜你喜欢
随机推荐
[Baidu map API] the version of the map JS API you are using is too low and no longer maintained. In order to ensure the normal use of the basic functions of the map, please upgrade to the latest versi
The profound meaning of unlimited ecological development in Poka -- Multidimensional Interpretation of parallel chain
Wechat applet reports an error request:fail -2:net:: err_ FAILED
Flink函数(2):CheckpointedFunction
Flink Watermark机制
首届波卡黑客松项目「Manta Network」的进击之路
[data mining] zero foundation entry decision tree
XML之建模
Imitate Baidu API of Baidu map page of a website
稀缺性之于Web3:如何成为去中心化世界的胜利者
Logic development analysis of LP dual currency liquidity pledge mining system
盘点波卡生态潜力项目 | 跨链特性促进多赛道繁荣
【activiti】流程变量
关于DAO流动性双币质押挖矿开发原理分析
【vsphere高可用】主机和虚拟机故障监测工作原理
微信小程序报错request:fail -2:net::ERR_FAILED
在本地怎么使用phpstudy搭建WordPress网站
Insanity:1 (insanity hosting) target penetration vulnhub
Creation and generation of SVG format map in Heilongjiang Province
Useref create dynamic reference









