在记录操作日志的时候要用到操作用户的用户名,可以通过线程局部变量来获取。
从用户请求的token令牌中解析出用户名,放入线程局部变量,用到的时候直接从中取出。
BaseContext
/**
* @Description: 线程局部变量
* @Author: 翰戈.summer
* @Date: 2023/11/17
* @Param:
* @Return:
*/
public class BaseContext {
public static ThreadLocal<String> threadLocal = new ThreadLocal<>();
public static void setContext(String context) {
threadLocal.set(context);
}
public static String getContext() {
return threadLocal.get();
}
public static void removeContext() {
threadLocal.remove();
}
}