No thread-bound request found


最近在给同事调试项目时,发现一个问题,在Security Success 回调逻辑中调用RequestContextHolder.currentRequestAttributes() 出现报错:

caused by :java.lang.IllegalStateException: No thread-bound request found: 
  Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? 
If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: 
In this case, use RequestContextListener or RequestContextFilter to expose the current request.

 

出现这个问题时,根据字面意思以为是回调的Request不在同一个线程,也与其他项目进行了比较,依旧无果,浪费了不少脑细胞。。。

 

在Debug跟进代码查看时发现没有加载RequestContextFilter(在SpringBoot中@EnableAutoConfiguration会创建该对象)对象,又翻了一遍项目发现配置了@EnableWebMvc + extends WebMvcConfigurationAdapter,这个会使SpringBoot的@EnableAutoConfiguration失效,----- 问题找到啦 👏👏👏

 

因为有些项目需要添加自己的一些配置,所以需要重写一些启动配置逻辑,大概分为以下3种:

  1、@EnableWebMvc + extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式会屏蔽SpringBoot的@EnableAutoConfiguration

  2、extends WebMvcConfigurationSupport,在扩展的类中重写父类的方法即可,这种方式会屏蔽SpringBoot的@EnableAutoConfiguration

  3、extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式则不会屏蔽SpringBoot的@EnableAutoConfiguration

具体使用哪种还是根据项目需要来选择。

就此,填坑结束!!!

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM