欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2022-10-18 11:53:32|栏目:JAVA代码|点击:

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);
    }
}

上一篇:多模块maven的deploy集成gitlab ci自动发版配置

栏    目:JAVA代码

下一篇:解决因jdk版本引起的TypeNotPresentExceptionProxy异常

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

本文地址:http://www.codeinn.net/misctech/216645.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有