OpenFeign丢失请求头
ZYHWJL

由于OpenFeign通过HttpClient构造请求,请求头需手动设置

1
2
3
4
5
6
7
8
9
10
11
12
@Bean
public RequestInterceptor requestInterceptor(){
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String cookie = request.getHeader("Cookie");
template.header("Cookie",cookie);
}
};
}

OpenFeign在构造请求前会逐一调用RequestInterceptor的apply,故向SPringBean中注入RequestInterceptor并在apply中塞入header即可