zoukankan      html  css  js  c++  java
  • spring cloud feign 添加headers

    原文地址: https://www.jianshu.com/p/dfec934b737f

    很多时候我们需要feign的时候添加headers

    1、把当前登录用户的token传到下一个服务
    2、在自己的token里面传入参数,来表名自己是哪个服务去调用feign。以便让feign提供者识别请求者,做权限校验。

    import feign.RequestInterceptor;
    import feign.RequestTemplate;
    import org.springframework.stereotype.Service;
    
    @Service
    public class MyRequestInterceptor implements RequestInterceptor {
        @Override
        public void apply(RequestTemplate template) {
            template.header("my-header","header");
        }
    }
    

    加上这段就能让所有的feign请求都要经过这个拦截器

    原理

    SynchronousMethodHandler这个类里面的targetRequest构造request请求的时候会调用所有拦截器的apply方法。

      Request targetRequest(RequestTemplate template) {
        for (RequestInterceptor interceptor : requestInterceptors) {
          interceptor.apply(template);
        }
        return target.apply(new RequestTemplate(template));
      }
    
  • 相关阅读:
    弹性盒模型的实际应用
    大图滚动--这是精髓实例
    三级联动
    sql
    jsp2
    marquee
    人机五子棋(AI算法有瑕疵)
    Jsp1
    倒计时
    时间
  • 原文地址:https://www.cnblogs.com/yeyongjian/p/13529131.html
Copyright © 2011-2022 走看看