@FeignClient 微服務(wù)傳遞head

使用微服務(wù)的架構(gòu)開(kāi)發(fā)的項(xiàng)目,需要時(shí)常調(diào)用其他服務(wù)的接口,@FeignClient默認(rèn)的設(shè)置會(huì)導(dǎo)致請(qǐng)求頭
head丟失,重寫(xiě)請(qǐng)求攔截器,將head頭信息放入到新請(qǐng)求當(dāng)中
@Configuration
public class FeignClientsConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return;
        }
        HttpServletRequest request = attributes.getRequest();
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                 //新請(qǐng)求會(huì)導(dǎo)致長(zhǎng)度改變,過(guò)濾
                if ("content-length".equalsIgnoreCase(name)) {
                    continue;
                }
                //防止傳遞格式修改,過(guò)濾
                if ("accept-encoding".equalsIgnoreCase(name)) {
                    continue;
                }
                String value = request.getHeader(name);
                requestTemplate.header(name, value);
            }
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容