千家信息网

SpringCloud Feign请求头如何删除修改

发表于:2025-01-16 作者:千家信息网编辑
千家信息网最后更新 2025年01月16日,本篇内容主要讲解"SpringCloud Feign请求头如何删除修改",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"SpringCloud Feign请求
千家信息网最后更新 2025年01月16日SpringCloud Feign请求头如何删除修改

本篇内容主要讲解"SpringCloud Feign请求头如何删除修改",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"SpringCloud Feign请求头如何删除修改"吧!

Feign请求头修改删除操作

@Configurationpublic 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 contentTypeList = Lists.newArrayList("application/json;charset=UTF-8");                    Map> headers = ImmutableMap.of(HttpHeaders.CONTENT_TYPE, contentTypeList);                    template.headers(headers);                }            }        };    }    protected static void mineHeadersHandle(MimeHeaders mimeHeaders) {        mimeHeaders.removeHeader(HttpHeaders.CONTENT_TYPE);    }}

到此,相信大家对"SpringCloud Feign请求头如何删除修改"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0