版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、spring 中使用自定义的 threadlocal 存储导致的坑spring 中有时候我们需要存储一些和 request 相关联的变量,例如用户的登陆有关信息等,它的生命周期和 request 相同。一个简单想到的实现方法是用法 threadlocal:public class securitycontextholder private static final threadlocal securitycontext = new threadlocal();public static void set(securitycontext context) securitycontext.set(
2、context);public static securitycontext get() return securitycontext.get();public static void clear() securitycontext.remove();用法一个自定义的 handlerinterceptor 将有关信息注入进去:slf4jcomponentpublic class requestinterceptor implements handlerinterceptor overridepublic boolean prehandle(httpservletrequest request,
3、 httpservletresponse response, object handler) throwsexception try securitycontextholder.set(retrieverequestcontext(request); catch (exception ex) log.warn("读取哀求信息失败", ex);return true;overridepublic void posthandle(httpservletrequest request, httpservletresponse response, object ha
4、ndler, nullablemodelandview modelandview) throws exception securitycontextholder.clear();通过这样,我们就可以在 controller 中挺直用法这个 context,很便利的猎取到有关用户的信息:slf4jrestcontrollerclass controller public result get() long userid = securitycontextholder.get()。getuserid();/这个办法也是无数博客中用法的。然而这个办法却存在着一个很隐蔽的坑: handlerinter
5、ceptor 的 posthandle 并不总是会调用。当 controller 中浮现 exception:slf4jrestcontrollerclass controller public result get() long userid = securitycontextholder.get()。getuserid();/throw new runtimeexception();或者在 handlerinterceptor 的 prehandle 中浮现 exception:slf4jcomponentpublic class requestinterceptor implements
6、 handlerinterceptor overridepublic boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throwsexception try securitycontextholder.set(retrieverequestcontext(request); catch (exception ex) log.warn("读取哀求信息失败", ex);/throw new runtimeexception();
7、/return true;这些状况下, posthandle 并不会调用。这就导致了 threadlocal 变量不能被清理。在平时的 java 环境中,threadlocal 变量随着 thread 本身的销毁,是可以被销毁掉的。但 spring 因为采纳了线程池的设计,响应哀求的线程可能会向来常驻,这就导致了变量向来不能被 gc 回收。更糟糕的是,这个没有被正确回收的变量,因为线程池对线程的复用,可能会串到别的 request 当中,进而挺直导致代码规律的错误。为了解决这个问题,我们可以用法 spring 自带的 requestcontextholder ,它背后的原理也是 threadl
8、ocal,不过它总会被更底层的 servlet 的 filter 清理掉,因此不存在泄露的问题。下面是一个用法 requestcontextholder 重写的例子:public class securitycontextholder private static final string security_context_attributes = "security_context"public static void setcontext(securitycontext context) requestcontextholder.currentreque
9、stattributes()。setattribute(security_context_attributes,context,requestattributes.scope_request);public static securitycontext get() return (securitycontext)requestcontextholder.currentrequestattributes()。getattribute(security_context_attributes, requestattributes.scope_request);除了用法 requestcontextholder 还可以用法 reque
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 合肥信息技术职业学院《材料成形工艺基础》2025-2026学年期末试卷
- 2026年陕西省西安市社区工作者招聘笔试模拟试题及答案解析
- 福建卫生职业技术学院《工程数学》2025-2026学年期末试卷
- 2026年泸州市纳溪区社区工作者招聘笔试参考试题及答案解析
- 2026年河南省濮阳市城管协管招聘笔试备考题库及答案解析
- 2026年枣庄市山亭区社区工作者招聘考试模拟试题及答案解析
- 2026年南昌市湾里区社区工作者招聘考试备考题库及答案解析
- 2026年四川省宜宾市社区工作者招聘考试备考试题及答案解析
- (新)食品安全管理规章制度(食品经营许可证)(3篇)
- 2026年湘潭市岳塘区社区工作者招聘笔试参考试题及答案解析
- 2026年北京市西城区高三一模历史试卷(含答案)
- 学校考试评价工作制度
- 岳阳市湘阴县重点名校2026届中考数学全真模拟试卷含解析
- 2025浙能集团甘肃有限公司新能源项目(第二批)招聘17人笔试历年难易错考点试卷带答案解析
- 2026年美术鉴赏学习通测试题及答案
- 2025天猫香氛身体护理白皮书
- 2026山东青岛海洋地质工程勘察院有限公司招聘2人笔试备考试题及答案解析
- 浙教版小学五年级劳动下册项目一+任务二+风筝的制作(教学课件)
- 2026年阿拉善职业技术学院单招职业技能考试题库附参考答案详解(夺分金卷)
- 2026江西省海济融资租赁股份有限公司社会招聘2人笔试备考题库及答案解析
- 涉医风险内部报告制度
评论
0/150
提交评论