当前位置:网站首页>ThreadLocal stores the current login user information
ThreadLocal stores the current login user information
2022-07-24 05:47:00 【Qinglin emo】
ThreadLocal User information can be saved in the thread , When the request is over, we are clearing the saved information . In this way, when we develop, we can directly from the overall situation ThreadLocal It is very convenient to obtain user information in
/** * Save the name of the user object ThreadLocal * * @author wangql */
@Component
public class UserInfoThreadHolder {
private static final ThreadLocal<SysUser> userThreadLocal = new ThreadLocal<>();
/** * Add the currently logged in user method */
public static void addCurrentUser() {
// The login object is obtained from here and set Into the ThreadLocal
// According to the business logic
SysUser sysUser = new SysUser();
userThreadLocal.set(sysUser);
}
public static SysUser getCurrentUser() {
return userThreadLocal.get();
}
/** * Prevent memory leaks */
public static void remove() {
userThreadLocal.remove();
}
}
Interceptors can be used to operate
/** * Save the login user into 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;
}
/** * Avoid memory leaks */
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
UserInfoThreadHolder.remove();
}
}
Register interceptors
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private LoginInterceptor loginInterceptor;
// Register interceptors
@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;
}
/** * Configure cross domain * @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);
}
}
边栏推荐
- 【数据挖掘】聚类分析的简要介绍
- 【activiti】activiti环境配置
- 【vsphere高可用】主机故障切换
- Flink Format系列(1)-JSON
- 【虚拟化】如何将虚拟机从workstation转换到esxi
- Recommend a fully open source, feature rich, beautiful interface mall system
- Imitate Baidu API of Baidu map page of a website
- Flink Watermark机制
- Multi merchant mall system function disassembly lecture 07 - platform side commodity management
- 使用bat命令快速创建系统还原点的方法
猜你喜欢

likeshop单商户SAAS商城系统搭建,代码开源无加密。

【activiti】网关

【activiti】个人任务

Insanity:1 (insanity hosting) target penetration vulnhub

Multi merchant mall system function disassembly lecture 06 - platform side merchant settlement agreement

The female colleague of the company asked me to go to her residence to repair the computer at 11 o'clock at night. It turned out that disk C was popular. Look at my move to fix the female colleague's

【activiti】组任务

Flink 时间流处理

likeshop单商户SAAS商城系统无限多开

Flink Task、Sub-Task、task slot和parallelism
随机推荐
首届波卡黑客松项目「Manta Network」的进击之路
MySQL queries the last four digits of the mobile phone number. How to write the first few digits?
Multi merchant mall system function disassembly lecture 09 - platform end commodity brands
达梦数据库_触发器、视图、物化视图、序列、同义词、自增列、外部链接等基本的操作
Inventory Poka ecological potential project | cross chain characteristics to promote the prosperity of multi track
使用bat命令快速创建系统还原点的方法
Flink Watermark机制
[vSphere high availability] virtual machine reset and restart
多商户商城系统功能拆解10讲-平台端商品单位
Public chain Sui layer1 network
【mycat】mycat配置文件
Multi merchant mall system function disassembly lecture 07 - platform side commodity management
Gavin wood, founder of Poka: what will happen to Poka governance V2?
多商户商城系统功能拆解03讲-平台端商家管理
《机器学习》(周志华) 第4章 决策树 学习心得 笔记
【activiti】流程实例
Likeshop single merchant mall system is built, and the code is open source without encryption
Whole station downloader recommendation
Flink format series (1) -json
【vsphere高可用】主机出现故障或隔离后的处理