From 996fd7b32814dde91764a32a811701eaec45ed9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com> Date: Tue, 5 Apr 2022 22:16:38 +0800 Subject: [PATCH] =?UTF-8?q?fix(FeignConfig.class):=20feign=E6=8B=A6?= =?UTF-8?q?=E6=88=AA=E5=99=A8=E6=B7=BB=E5=8A=A0RequestAttributes=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E4=B8=BA=E7=A9=BA=E7=9A=84=E5=88=A4=E6=96=AD=EF=BC=8C?= =?UTF-8?q?MQ=E7=9B=91=E5=90=AC=E8=B0=83=E7=94=A8=E6=97=B6=E4=B8=BAnull?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../youlai/common/web/config/FeignConfig.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/youlai-common/common-web/src/main/java/com/youlai/common/web/config/FeignConfig.java b/youlai-common/common-web/src/main/java/com/youlai/common/web/config/FeignConfig.java index adc5db15b..2d114cf15 100644 --- a/youlai-common/common-web/src/main/java/com/youlai/common/web/config/FeignConfig.java +++ b/youlai-common/common-web/src/main/java/com/youlai/common/web/config/FeignConfig.java @@ -5,6 +5,7 @@ import feign.RequestInterceptor; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.servlet.DispatcherServlet; @@ -42,17 +43,19 @@ public class FeignConfig { @Bean public RequestInterceptor requestInterceptor() { return (template) -> { - ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder - .getRequestAttributes(); - HttpServletRequest request = attributes.getRequest(); - //获取请求头 - Enumeration headerNames = request.getHeaderNames(); - if (headerNames != null) { - while (headerNames.hasMoreElements()) { - String name = headerNames.nextElement(); - String values = request.getHeader(name); - //将请求头保存到模板中 - template.header(name, values); + RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); + if (requestAttributes != null) { + ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes; + HttpServletRequest request = attributes.getRequest(); + //获取请求头 + Enumeration headerNames = request.getHeaderNames(); + if (headerNames != null) { + while (headerNames.hasMoreElements()) { + String name = headerNames.nextElement(); + String values = request.getHeader(name); + //将请求头保存到模板中 + template.header(name, values); + } } } };