返回顶部
首页 > 资讯 > 后端开发 > Python >SpringCloud Feign请求头删除修改的操作代码
  • 937
分享到

SpringCloud Feign请求头删除修改的操作代码

2024-04-02 19:04:59 937人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

Feign请求头修改删除操作 @Configuration public class ClientConfiguration { @Bean public Reque

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请求头内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringCloud Feign请求头删除修改的操作代码

本文链接: https://lsjlt.com/news/143158.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作