• 微服务通过feign.RequestInterceptor传递参数


    原文链接:https://www.cnblogs.com/baizhanshi/p/10913590.html

    Feign 支持请求拦截器,在发送请求前,可以对发送的模板进行操作,例如设置请求头等属性,自定请求拦截器需要实现 feign.RequestInterceptor 接口,该接口的方法 apply 有参数 template ,该参数类型为 RequestTemplate,我们可以根据实际情况对请求信息进行调整,示例如下:

    • 创建自定义请求拦截器,在发送请求前增加了一个请求头信息,进行身份校验。

    具体代码参考如下:

    import feign.RequestInterceptor;
     
    import feign.RequestTemplate;
     
        
     
    public class MyRequestInterceptor implements RequestInterceptor{
     
        
     
    public void apply(RequestTemplatetemplate){
     
    template.header("Authorization","123");
     
    }
     
    }

     服务端可以通过HttpServletRequest获取到前面传递的参数,具体获取逻辑如下:

    RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
           if (requestAttributes != null) {
               HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
               request.getHeader("Authorization");
           }

    就实现了各个微服务之间参数的传递。 

  • 相关阅读:
    MySQL--单表查询
    python库--pandas--Series.str--字符串处理
    如何 grep tab & 如何grep 减号(dash)
    png压缩
    如何无密码登陆远程机器?
    ssh中运行awk
    PHP 时区
    sublime使用
    nginx 50x故障分析
    nginx反向代理异常
  • 原文地址:https://www.cnblogs.com/fswhq/p/13691075.html
Copyright © 2020-2023  润新知