fix(FeignConfig.class): feign拦截器添加RequestAttributes是否为空的判断,MQ监听调用时为null

This commit is contained in:
郝先瑞 2022-04-05 22:16:38 +08:00
parent c7bb7d206f
commit 996fd7b328

View File

@ -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,8 +43,9 @@ public class FeignConfig {
@Bean
public RequestInterceptor requestInterceptor() {
return (template) -> {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes != null) {
ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
HttpServletRequest request = attributes.getRequest();
//获取请求头
Enumeration<String> headerNames = request.getHeaderNames();
@ -55,6 +57,7 @@ public class FeignConfig {
template.header(name, values);
}
}
}
};
}
}