为了避免微服务“球形”调用、降低耦合,把最近做的的项目分成 consumer 和 provider 但是在做token检验时发现request无法传递到provider,经过一番“面向搜索编程” 和 查看源码发现请求头被feign拦截了…
解决办法:
1.在consumer配置feign
@Slf4j
@Configuration
public class FeignConfiguration implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String values = request.getHeader(name);
template.header(name, values);
}
log.info("feign interceptor error header:{}",template);
}
}
}
2.yml配置信息
3.@FeignClient注解上加入第一步的配置文件
搞定