content: package com.open.capacity.config;
  
  import feign.RequestInterceptor;
  import feign.RequestTemplate;
  import org.apache.commons.lang3.StringUtils;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.stereotype.Component;
  import org.springframework.web.context.request.RequestContextHolder;
  import org.springframework.web.context.request.ServletRequestAttributes;
  
  import javax.servlet.http.HttpServletRequest;
  
  /**
   * @Author: [gitgeek]
   * @Date: [2018-07-12 11:32]
   * @Description: [ ]
   * @Version: [1.0.0]
   * @Copy: [com.zzg]
   */
  @Component
  @Configuration
  public class FeignConfig implements RequestInterceptor {
  
      private static final String TOKEN_KEY = "access_token";
  
      @Override
      public void apply(RequestTemplate requestTemplate) {
          requestTemplate.header("access_token", getToken(getHttpServletRequest()));
      }
  
      private HttpServletRequest getHttpServletRequest() {
          try {
              return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
          } catch (Exception e) {
              return null;
          }
      }
  
  //    private Map<String, String> getHeaders(HttpServletRequest request) {
  //        Map<String, String> map = new LinkedHashMap<>();
  //        Enumeration<String> enumeration = request.getHeaderNames();
  //        while (enumeration.hasMoreElements()) {
  //            String key = enumeration.nextElement();
  //            String value = request.getHeader(key);
  //            map.put(key, value);
  //        }
  //        return map;
  //    }
  
      /**
       * 根据参数或者header获取token
       *
       * @param request
       * @return
       */
      public static String getToken(HttpServletRequest request) {
          String token = request.getParameter(TOKEN_KEY);
          if (StringUtils.isBlank(token)) {
              token = request.getHeader(TOKEN_KEY);
          }
  
          return token;
      }
  
  
  }
  