**
用户登录拦截器查询到登录用户信息后将用户信息传递到后面的Controller
开发过程中,移动端提出能不能不要每次接口都传递用户信息,做下统一处理,于是乎我就想到了在@Aspect层将验证token时查到的用户信息保存到HttpServletRequest 中,结合了HttpServletRequest request的生命周期特征以及@Aspect的特性,我的想法得到了验证,确有这种方法用来传递登录用户的信息,结合业务场景。我们在拦截器中把用户信息放入request对象中,然后在后面的Controller中就可以在request中取到用户信息,再强转成用户对象即可。具体代码如下:
用户登录拦截器代码;
package com.lianjia.user.aspect;
//包名太长了影响阅读,所以删了
/**
* http请求切面
* @date: 2020/11/01
*/
@Aspect
@Component
public class HttpAspect {
@Autowired
private RedisUtilsServiceImpl redisUtilsService;
@Autowired
private UsersMapper usersMapper;
private final static Logger logger = LogManager.getLogger(HttpAspect.class);
private ThreadLocal<Long> threadLocal = new ThreadLocal<>();
@Pointcut("execution(public * com.project.user.controller.*.*(..))")
public void log() {
}
/**
* 环绕通知处理方法
*/
@Around("log()")
public Object doConcurrentOperation(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.