Skip to content

Instantly share code, notes, and snippets.

@0532
Created January 9, 2019 03:32
Show Gist options
  • Save 0532/646758b2b3bca3cbd0880245cc8c466e to your computer and use it in GitHub Desktop.
Save 0532/646758b2b3bca3cbd0880245cc8c466e to your computer and use it in GitHub Desktop.
feign请求copy 请求头,使用方法@FeignClient(url = "${feign.url.internal.securityService}", name = "deviceFingerPrintFeign" , configuration = FingerPrintFeignconfig.class)
@Configuration
public class FingerPrintFeignconfig implements RequestInterceptor {
private final Logger logger = LoggerFactory.getLogger(getClass());
@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);
}
logger.info("feign interceptor header:{}",template);
}
Enumeration<String> bodyNames = request.getParameterNames();
StringBuffer body =new StringBuffer();
if (bodyNames != null) {
while (bodyNames.hasMoreElements()) {
String name = bodyNames.nextElement();
String values = request.getParameter(name);
body.append(name).append("=").append(values).append("&");
}
}
if(body.length()!=0) {
body.deleteCharAt(body.length()-1);
template.body(body.toString());
logger.info("feign interceptor body:{}",body.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment