版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年湖南湘潭湘乡市粮油购销有限责任公司招聘3名市场化聘用工作人员考试备考题库及答案详解
- 2026年贵州长江汽车有限公司招聘12人笔试备考试题及答案详解
- 职业卫生技术服务专业技术人员考试(职业卫生检测)模拟题库及答案(湛江2026年)
- 2026西安市第四十二中学招聘高中物理教师考试备考试题及答案详解
- 2026年7月广西梧州市苍梧县城镇公益性岗位人员招聘5人笔试参考题库及答案详解
- 2026下半年四川内江市市中区紧密型县域医共体部分成员单位招聘员额(编外)人员60人笔试备考试题及答案详解
- 2026中国近现代科技史研究室招聘特别研究助理(博士后或项目聘用人员)1人考试备考试题及答案详解
- 职业安全健康培训考试试题及答案
- 2026广西罗城仫佬族自治县审计局招聘工作人员1人笔试模拟试题及答案详解
- 2025年伊春市红星区社区工作者招聘考试试题及答案详解
- 2026年安徽白帝集团有限公司社会招聘7人笔试参考试题及答案详解
- 2026年贵州省算力科技有限责任公司第一批人员招聘20人笔试备考题库及答案详解
- 肺结节诊治中国专家共识(2024年版)解读课件
- 2026年执业医师乡村全科执业助理医师考试真题(完整版)
- 2025年教师招聘考试《教育综合知识》教育写作题真题及答案
- 雨课堂学堂在线学堂云《景观水文(北京林业)》单元测试考核答案
- 《硬措施》解析培训课件-2024年
- 工作资质证明
- 施工电梯垂直度测量记录表
- 数学中考复习-半角模型课件
- 公司领导拜访政府的函
评论
0/150
提交评论