当前位置:网站首页>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);
}
}
边栏推荐
- 多商户商城系统功能拆解06讲-平台端商家入驻协议
- Interpretation of the randomness of POS mechanism, how does poca's randomness principle work?
- [data mining] zero foundation entry decision tree
- Mysqldump export Chinese garbled code
- Flink watermark mechanism
- The way to attack the first poca hackson project "Manta network"
- Analysis of logic development principle of quantitative contract clip arbitrage robot system
- The method of using bat command to quickly create system restore point
- Multi merchant mall system function disassembly lecture 09 - platform end commodity brands
- 【mycat】mycat介绍
猜你喜欢

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

Insanity:1 (insanity hosting) target penetration vulnhub

多商户商城系统功能拆解05讲-平台端商家主营类目

The repetition detection function of PHP multi line text content and count the number of repetitions

多商户商城系统功能拆解09讲-平台端商品品牌

haclabs: no_ Name (hl.ova) target penetration vulnhub

Canal+kafka实战(监听mysql binlog实现数据同步)

Likeshop single merchant mall system is built, and the code is open source without encryption

Are you still trying to limit MySQL paging?

【mycat】mycat搭建读写分离
随机推荐
Oracle数据库的逻辑结构
Inventory Poka ecological potential project | cross chain characteristics to promote the prosperity of multi track
The method of using bat command to quickly create system restore point
Multi merchant mall system function disassembly Lecture 10 - platform end commodity units
Subsystem technology and ecology may memorabilia | square one plan launched, Boca launched xcm!
Authorized access to MySQL database
Multi merchant mall system function disassembly lecture 05 - main business categories of platform merchants
达梦数据库_用户口令策略
数据集成的两种架构:ELT和ETL
Flink函数(2):CheckpointedFunction
[data mining] zero foundation entry decision tree
Are you still trying to limit MySQL paging?
Analysis of Dao liquidity dual currency pledge mining development principle
ERP+RPA 打通企业信息孤岛,企业效益加倍提升
Whole station downloader recommendation
对接CRM系统和效果类广告,助力企业精准营销助力企业精准营销
达梦数据库_在使用过程中的需要注意的小事项
推荐一款完全开源,功能丰富,界面精美的商城系统
Flink task, sub task, task slot and parallelism
mysql数据库的授权访问