版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小学语文一至六年级成语汇编(人教版)
- 期末检测题(试题)-2024-2025学年六年级上册数学人教版
- 仪器分析(山东联盟-青岛农业大学)智慧树知到答案2024年青岛农业大学
- 钦州市2024年春季学期高一期末教学质量监测+语文答案
- 材料采购服务方案(技术方案)
- 《茅屋为秋风所破歌》教案
- 江苏省盐城市盐城八校2024-2025学年高三上学期开学考试英语试题(原卷版)
- 联系实际讨论政府经济活动中公平与效率的关系以及公平与效率的选择
- 离心泵的运行管理
- 《蜜蜂绘春》幼儿园小学少儿美术教育绘画课件创意教程教案
- 几何图形课件青岛版数学七年级上册
- 皮内注射操作并发症处理介绍课件
- 外研版英语小学二年级上册全册课件
- 档案资源普查档案三合一制度编审培训会强调
- 中风患者的护理查房课件
- 应用品管圈降低胃管的非计划性拔管率课件
- 机加工费用计算-机械加工费报价明细及计算表
- 重庆银行总行科技部2023年2月社会招聘笔试历年高频考点试题答案详解
- 比亚迪宋PLUS EV说明书
- 上海华东师大二附中2024年高三5月模拟考英语试题含解析
- 附件一远程会诊终端设备一批
评论
0/150
提交评论