当前位置:网站首页>ThreadLocal 项目中实战
ThreadLocal 项目中实战
2022-07-20 08:10:00 【WuSong1999】
ThreadLocal的作用是在同一个线程共享数据,项目中每一个请求进来,从始至终都是同一个线程,在同一个线程中上一个线程放在内存中的数据如果要和下一个线程进行共享,那么就可以使用ThreadLocal
ThreadLocal的核心原理就是,它底层是一个Map<Thread,Object> threadLocal结构
/**
* 在执行目标方法前,先判断用户登录状态。并封装用户信息传递给Controller
*/
public class CartInterceptor implements HandlerInterceptor {
public static ThreadLocal<UserInfoTo> threadLocal = new ThreadLocal<>();
/*
目标方法之前拦截
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
UserInfoTo userInfoTo = new UserInfoTo();
HttpSession session = request.getSession();
MemberRespVo member = (MemberRespVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
if (member != null) {
//用户登录
userInfoTo.setUserId(member.getId());
}
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
String name = cookie.getName();
if (name.equals(CartConstant.TEMP_USER_COOKIE_NAME)) {
userInfoTo.setUserKey(cookie.getValue());
userInfoTo.setTempUser(true);
}
}
}
//目标方法执行之前
//如果没有临时用户,分配临时用户
if (StringUtils.isEmpty(userInfoTo.getUserKey())) {
String uuid = UUID.randomUUID().toString();
userInfoTo.setUserKey(uuid);
}
threadLocal.set(userInfoTo);
return true;
}
/*
业务执行之后 分配临时用户
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
UserInfoTo userInfoTo = threadLocal.get();
//如果没有任何信息,创建临时用户,保存cookie
if (!userInfoTo.isTempUser()) {
Cookie cookie = new Cookie(CartConstant.TEMP_USER_COOKIE_NAME, userInfoTo.getUserKey());
cookie.setDomain("gulimall.com");
//单位秒
cookie.setMaxAge(CartConstant.TEMP_USER_COOKIE_TIMEOUT);
response.addCookie(cookie);
}
}
}
边栏推荐
- 几个DPDK控制框架
- 比特置不可用——文件或目錄損壞且無法讀取
- Is it far from working in the "meta universe"? "Face pincher" is nearby
- TP5.1 initialize初始化方法(不是_initialize)
- Use r to delete rows with empty cells from Excel
- R语言快速绘制多因素回归分析森林图
- Reducing Participation Costs via Incremental Verification for Ledger Systems学习笔记
- Aruba学习笔记02-Web UI --Dashbord面板介绍
- 逆向分析工具IDA与开源工具Ghidra、Cutter对比测评
- R language world values survey assignment
猜你喜欢
Mysql 常用函数汇总,很实用,面试时常会遇到
Qt 多线程实现的两种方式 线程实现
Program yuan sent an article lamenting unemployment, which attracted program apes from various industries to comfort, laughing in the comment area
PO,BO,VO,DTO和POJO的概念区分
Flink 的学习笔记
Run Keyword If 使用详解
渗透测试-SQL注入之sqlmap的使用方法及实战案例
Jenkins uses the uploaded file parameter as a parameter
tp5.1 控制器初始化initialize(继承公共文件初始化)
Scala变量和数据类型(01)
随机推荐
控制台C#飞行棋小项目
研究一下 JSON.parse(JSON.stringify(obj))
Mysql 常用函数汇总,很实用,面试时常会遇到
ASP. Net core usage record 2
R语言世界价值观调查作业
Study json parse(JSON.stringify(obj))
tp5.0 后台admin访问
Duplicate class com. amap. api. fence. DistrictItem found in modules jetified-3dmap-9.3.1. jar (com.amap.
Routing of upper layer loops in VRRP
Detailed explanation of x509 digital certificate
uniapp使用uni.request请求报错{“errMsg“:“request:fail abort statusCode:-1“}的解决办法
经典网络学习-ResNet代码实现
sql递归随记
Search engines fall in Web3.0?
使用R从Excel中删除带有空单元格的行
ASP.NET CORE 自定义中间件
JS 将几个对象存入数组中的简便写法
Duplicate class com.amap.api.fence.DistrictItem found in modules jetified-3dmap-9.3.1.jar (com.amap.
Introduction to Lua scripting language
The best method of converting string to date in R language