溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用Feign傳遞請求頭信息

發(fā)布時間:2022-03-07 11:56:45 來源:億速云 閱讀:744 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)如何使用Feign傳遞請求頭信息,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

    Feign傳遞請求頭信息

    在我之前的文章服務(wù)網(wǎng)關(guān)Spring Cloud Zuul中,將用戶的登錄id放在了請求頭中傳遞給內(nèi)部服務(wù)。

    但是當(dāng)內(nèi)部服務(wù)之間存在feign調(diào)用時,那么請求頭信息會在feign請求的時候傳遞嗎?不會,請求的頭信息和請求參數(shù)都不會進行傳遞。

    但是我們可以通過通過實現(xiàn)RequestInterceptor接口,完成對所有的Feign請求,傳遞請求頭和請求參數(shù)。

    實現(xiàn)RequestInterceptor接口

    import feign.RequestInterceptor;
    import feign.RequestTemplate;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    import javax.servlet.http.HttpServletRequest;
    import java.util.Enumeration;
    /**
     * Feign請求攔截器(設(shè)置請求頭,傳遞登錄信息)
     *
     * @author simon
     * @create 2018-09-07 9:51
     **/
    public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
      @Override
      public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
          while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            String values = request.getHeader(name);
            requestTemplate.header(name, values);
          }
        }
      }
    }

    這里只設(shè)置了請求頭,如果想傳遞請求參數(shù),可以參考如下代碼:

    public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
      @Override
      public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
          while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            String values = request.getHeader(name);
            requestTemplate.header(name, values);
          }
        }
        Enumeration<String> bodyNames = request.getParameterNames();
          StringBuffer body =new StringBuffer();
          if (bodyNames != null) {
              while (bodyNames.hasMoreElements()) {
                String name = bodyNames.nextElement();
                String values = request.getParameter(name);
                body.append(name).append("=").append(values).append("&");
              }
          }
         if(body.length()!=0) {
            body.deleteCharAt(body.length()-1);
            template.body(body.toString());
            logger.info("feign interceptor body:{}",body.toString());
        }
      }
    }

    注冊配置

    package com.southgis.ibase.personalConfigure.config;
    import com.southgis.ibase.utils.FeignBasicAuthRequestInterceptor;
    import com.southgis.ibase.utils.FeignSpringFormEncoder;
    import feign.RequestInterceptor;
    import feign.codec.Encoder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    /**
     * Feign配置注冊(全局)
     *
     * @author simon
     * @create 2018-08-20 11:44
     **/
    @Configuration
    public class FeignSupportConfig {
      /**
       * feign請求攔截器
       *
       * @return
       */
      @Bean
      public RequestInterceptor requestInterceptor(){
        return new FeignBasicAuthRequestInterceptor();
      }
    }

    這個文件放在項目的掃描目錄下,所有的feign調(diào)用都會使用此配置。如果只有某個feign調(diào)用則可以這樣設(shè)置(但配置類不能在掃描目錄下):

    @FeignClient(name = "organ",path = "/organ/OrganInfo",configuration = FeignSupportConfig.class)

    Feign調(diào)用微服務(wù)傳遞header請求頭

    package com.chitic.module.core.config;
    import feign.RequestInterceptor;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    import javax.servlet.http.HttpServletRequest;
    import java.util.Enumeration;
     
    @Configuration
    public class FeignConfig {
        @Bean
        public RequestInterceptor headerInterceptor() {
            return template -> {
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (null != attributes) {
                    HttpServletRequest request = attributes.getRequest();
                    Enumeration<String> headerNames = request.getHeaderNames();
                    if (headerNames != null) {
                        while (headerNames.hasMoreElements()) {
                            String name = headerNames.nextElement();
                            String values = request.getHeader(name);
                            template.header(name, values);
                        }
                    }
                }
            };
        }
    }

    需注意,feign調(diào)用時不能調(diào)用含有HttpServletResponse參數(shù)(比如常用的數(shù)據(jù)導(dǎo)出),以下就不能遠程調(diào)用,目前沒找到解決辦法

    如何使用Feign傳遞請求頭信息

    關(guān)于“如何使用Feign傳遞請求頭信息”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

    向AI問一下細節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI