Browse Source

feign 动态创建client,拦截器执行多次 #2275

pull/2310/head
zhangdaiscott 4 years ago
parent
commit
766c48bdbf
  1. 29
      jeecg-boot/jeecg-boot-starter/jeecg-boot-starter-cloud/src/main/java/org/jeecg/starter/cloud/feign/impl/JeecgFeignService.java

29
jeecg-boot/jeecg-boot-starter/jeecg-boot-starter-cloud/src/main/java/org/jeecg/starter/cloud/feign/impl/JeecgFeignService.java

@ -35,25 +35,26 @@ public class JeecgFeignService implements IJeecgFeignService {
.encoder(encoder)
.decoder(decoder)
.contract(contract);
builder.requestInterceptor(requestTemplate -> {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (null != attributes) {
HttpServletRequest request = attributes.getRequest();
log.info("Feign request: {}", request.getRequestURI());
// 将token信息放入header中
String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
if(token==null){
token = request.getParameter("token");
}
log.info("Feign request token: {}", token);
requestTemplate.header(CommonConstant.X_ACCESS_TOKEN, token);
}
});
}
@Override
public <T> T newInstance(Class<T> clientClass, String serviceName) {
builder.requestInterceptor(requestTemplate -> {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (null != attributes) {
HttpServletRequest request = attributes.getRequest();
log.info("Feign request: {}", request.getRequestURI());
// 将token信息放入header中
String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
if(token==null){
token = request.getParameter("token");
}
log.info("Feign request token: {}", token);
requestTemplate.header(CommonConstant.X_ACCESS_TOKEN, token);
}
});
return builder.target(clientClass, String.format("http://%s/", serviceName));
}
}
Loading…
Cancel
Save