1. 程式人生 > >Feign傳遞請求頭資訊

Feign傳遞請求頭資訊

在我之前的文章服務閘道器Spring Cloud Zuul中,將使用者的登入id放在了請求頭中傳遞給內部服務。但是當內部服務之間存在feign呼叫時,那麼請求頭資訊會在feign請求的時候傳遞嗎?不會,請求的頭資訊和請求引數都不會進行傳遞。但是我們可以通過通過實現RequestInterceptor介面,完成對所有的Feign請求,傳遞請求頭和請求引數。

1. 實現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請求攔截器(設定請求頭,傳遞登入資訊)
 *
 * @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);
      }
    }
  }
}

這裡只設置了請求頭,如果想傳遞請求引數,可以參考如下程式碼:

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

2. 註冊配置
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呼叫都會使用此配置。如果只有某個feign呼叫則可以這樣設定(但配置類不能在掃描目錄下):

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