当前位置:网站首页>ThreadLocal保存用户登录信息
ThreadLocal保存用户登录信息
2022-08-01 14:05:00 【独酌先生QAQ】
目录结构:

1.编写注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @Target 用于描述注解的使用范围 ElementType.METHOD 用于描述方法 ElementType.TYPE 用于描述类、接口(包括注解类型) 或enum声明
* @Retention 定义被它所注解的注解保留多久
* source:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;被编译器忽略
* class:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期
* runtime:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExposeUserInfo {
}2.编写类user,ThreadLocal存放user类
import lombok.Data;
@Data
public class User {
String id;
String name;
}
public class UserContext {
private static ThreadLocal<User> threadLocal = new ThreadLocal<>();
public static void set(User user) {
threadLocal.set(user);
}
public static User get() {
return threadLocal.get();
}
public static void remove(){
threadLocal.remove();
}
}3.编写切面
import com.example.aopannotation.threadlocal.User;
import com.example.aopannotation.threadlocal.UserContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Aspect
@Component
public class UserInfoAspect {
public Logger logger = LoggerFactory.getLogger(UserInfoAspect.class);
/**
* 切点放在注解上,通过注解实现aop切面
*/
@Pointcut("@annotation(com.example.aopannotation.annotation.ExposeUserInfo)")
public void pointcut() {
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
User user = new User();
//1.从请求头获取用户的信息
HttpServletRequest request = null;
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
request = requestAttributes.getRequest();
// 2.获取用户的id
String id = request.getHeader("id");
//此处可以查库,得到user对象的其他信息
user.setId(id);
UserContext.set(user);
try {
// 3.设置用户上下文
UserContext.set(user);
// 执行并返回
return joinPoint.proceed();
} catch (Throwable throwable) {
throw throwable;
} finally {
// 4.移除(防止内存泄漏)
UserContext.remove();
}
}
}4.编写controller
import com.example.aopannotation.annotation.ExposeUserInfo;
import com.example.aopannotation.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class ExposeController {
@Autowired
UserInfoService userInfoService;
@ExposeUserInfo
@PostMapping("/userinfo")
public String test() {
return userInfoService.getUserInfo();
}
}5.编写service
import com.example.aopannotation.threadlocal.User;
import com.example.aopannotation.threadlocal.UserContext;
import org.springframework.stereotype.Service;
@Service
public class UserInfoService {
public String getUserInfo(){
User user= UserContext.get();
return user.getId();
}
}
6.postman请求

边栏推荐
- postgresql之page分配管理(二)
- HTB-Mirai
- 2022图片在线加水印源码
- How does the SAP ABAP OData service support the Create operation trial version
- 【无标题】
- [深入研究4G/5G/6G专题-47]: 5G Link Adaption链路自适应-3-下行链路自适应DLLA-PDSCH信道
- What is consistent hashing?In what scenarios can it be applied?
- 论文详读《基于改进 LeNet-5 模型的手写体中文识别》,未完待补充
- 什么是元编程
- PIR人体感应AC系列感应器投光灯人体感应开关等应用定制方案
猜你喜欢
随机推荐
[Binary Tree] Path Sum II
【无标题】
台积电认清了形势,新的建厂计划没有美国,中国芯片也得到重视
Qt实战案例(55)——利用QDir删除选定文件目录下的空文件夹
倪光南:openEuler已达国际同类社区水准
207.数组序号转换
iPhone难卖,被欧洲反垄断的服务业务也难赚钱了,苹果的日子艰难
【每日一题】1331. 数组序号转换
超全!全国近90所大学考研报录比汇总!
分布式中的远程调用
Gradle series - Gradle tests, Gradle life cycle, settings.gradle description, Gradle tasks (based on Groovy documentation 4.0.4) day2-3
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
AtCoder Beginner Contest 261 D - Flipping and Bonus
Six Stones Programming: Problems must be faced, methods must be skillful, and functions that cannot be done well must be solved
1161. 最大层内元素和
postgresql之page分配管理(二)
Efficiency tools to let programmers get off work earlier
HTB-Shocker
使用ffmpeg来查看视频的信息,fps,和width,height
预防和制止家庭暴力 人身安全保护令司法解释今起施行









