这里需要配置一个Bean,代码如下
@Configuration
public class FeignConfiguration {
@Bean
public RequestInterceptor requestInterceptor(){
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes!=null){
HttpServletRequest request = attributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames!=null){
while (headerNames.hasMoreElements()){
String headerName = headerNames.nextElement();
String header = request.getHeader(headerName);
requestTemplate.header(headerName,header);
}
}
}
}
};
}
}
大概意思就是,将前端传递过来的请求头,原封不动的通过feign远程调用传递给服务被调用方。
|