为了防止提交表单的时候携带的个人信息被篡改,我们经常使用token保存用户的个人信息,每次调用都是传token解析出个人信息,保存在当前的线程中,在哪里使用了我们就在线程中取出,防止提交人和个人信息不一致。
在使用线程池上下文存取用户信息在 当前线程中接口登录的时候获取token 解析token信息 将用户信息添加TransmittableThreadLocal的实例中,防止提交的用户信息被篡改。在接口中使用到用户信息的时候可以在上下文中取出,当前线程使用结束需求清除掉。清除的原因防止添加多次内存一直占用,如果遇到使用相同线程的业务会取到其他人存入的用户信息。比如我目前用到定时任务用的@Schedule,他实现的使用的相同的线程,当前一个任务结束用户信息添加了没有删除,下一个任务获取的就是上个任务的用户信息,坑惨了。
TransmittableThreadLocal 优势在于主线程中设置的值在子线程中可以获取到,需要注意对线程中使用后需要回收。
工具类
拦截每个请求 添加上下文 使用完成就需要清掉
接口中使用