SpringCloud Feign请求头删除修改的操作代码
短信预约 -IT技能 免费直播动态提醒
Feign请求头修改删除操作
@Configuration
public class ClientConfiguration {
@Bean
public RequestInterceptor headerInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
if (httpServletRequest.getRequestURI().contains(template.url())) {
try {
Field connectorField = ReflectionUtils.findField(SecurityContextHolderAwareRequestWrapper.class, "request");
connectorField.setAccessible(true);
FirewalledRequest firewalledRequest = (FirewalledRequest) connectorField.get(httpServletRequest);
if(Objects.isNull(firewalledRequest))
return;
Field firewalledField = ReflectionUtils.findField(FirewalledRequest.class, "request");
firewalledField.setAccessible(true);
RequestFacade requestFacade = (RequestFacade) firewalledField.get(firewalledRequest);
if(Objects.isNull(requestFacade))
return;
connectorField = ReflectionUtils.findField(RequestFacade.class, "request");
firewalledField.setAccessible(true);
Request connectorRequest = (Request) connectorField.get(requestFacade);
if(Objects.isNull(connectorRequest))
return;
Field coyoteField = ReflectionUtils.findField(Request.class, "coyoteRequest", org.apache.coyote.Request.class);
coyoteField.setAccessible(true);
org.apache.coyote.Request coyoteRequest = (org.apache.coyote.Request) coyoteField.get(connectorRequest);
if(Objects.isNull(coyoteRequest))
return;
Field mimeHeadersField = ReflectionUtils.findField(org.apache.coyote.Request.class, "headers", MimeHeaders.class);
mimeHeadersField.setAccessible(true);
MimeHeaders mimeHeaders = (MimeHeaders) mimeHeadersField.get(coyoteRequest);
mineHeadersHandle(mimeHeaders);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
List<String> contentTypeList = Lists.newArrayList("application/json;charset=UTF-8");
Map<String, Collection<String>> headers = ImmutableMap.of(HttpHeaders.CONTENT_TYPE, contentTypeList);
template.headers(headers);
}
}
};
}
protected static void mineHeadersHandle(MimeHeaders mimeHeaders) {
mimeHeaders.removeHeader(HttpHeaders.CONTENT_TYPE);
}
}
到此这篇关于SpringCloud Feign请求头删除修改的操作代码的文章就介绍到这了,更多相关SpringCloud Feign请求头内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341