实现Feign请求拦截器,对请求header等参数进行转发
问题:通过Feign远程调用服务,无法传递header参数。
解决方式:实现RequestInterceptor接口(对所有的Feign请求进行拦截,从request中取参数进行构造,主要代码:requestTemplate.header(name, values))
代码:
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
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 xxx
* @Date 2019/6/18 16:03
* 说明:服务间进行feign调用时,不会传递请求头信息。
* 通过实现RequestInterceptor接口,完成对所有的Feign请求,传递请求头和请求参数。
*/
@Component
public class FeignRequestInterceptor 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);
}
}
// 设置request中的attribute到header:主要是设置自行设置的token、userId等信息,以便转发到Feign调用的服务
Enumeration<String> reqAttrbuteNames = request.getAttributeNames();
if (reqAttrbuteNames != null) {
while (reqAttrbuteNames.hasMoreElements()) {
String attrName = reqAttrbuteNames.nextElement();
String values = request.getAttribute(attrName).toString();
requestTemplate.header(attrName, values);
}
}
}
}
注:“”设置request中的attribute到header....“”部分是当前使用的开发框架需要,没必要可删除那段代码。
通过 Enumeration<String> reqAttrbuteNames = request.getAttributeNames(); 这种方式获取request的attributes,前提是通过 servlteRequest.setAttribute("name", "value") 已设置了所需参数。
实现Feign请求拦截器,对请求header等参数进行转发的更多相关文章
- vue+axois 封装请求+拦截器(请求锁+统一错误)
需求 封装常用请求 拦截器-请求锁 统一处理错误码 一.封装常用的请求 解决痛点:不要每一个模块的api都还要写get,post,patch请求方法.直接将这些常用的方法封装好. 解决方案:写一个类 ...
- axios请求拦截器(修改Data上的参数 ==>把data上的参数转为FormData)
let instance = axios.create({ baseURL: 'http://msmtest.ishare-go.com', //请求基地址 // timeout: 3000,//请求 ...
- Feign 请求拦截器和日志
Feign 支持请求拦截器,在发送请求前,可以对发送的模板进行操作,例如设置请求头等属性,自定请求拦截器需要实现 feign.RequestInterceptor 接口,该接口的方法 apply 有参 ...
- http request 请求拦截器,有token值则配置上token值
// http request 请求拦截器,有token值则配置上token值 axios.interceptors.request.use( config => { if (token) { ...
- Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,拦截器Ajax请求
Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,拦截器Ajax请求 >>>>>>>>>>>>>>&g ...
- Vue添加请求拦截器
一.现象 统一处理错误及配置请求信息 二.解决 1.安装 axios , 命令: npm install axios --save-dev 2.在根目录的config目录下新建文件 axios.js ...
- spring mvc 通过拦截器记录请求数据和响应数据
spring mvc 能过拦截器记录请求数据记录有很多种方式,主要有以下三种: 1:过滤器 2:HandlerInterceptor拦截器 3:Aspect接口控制器 但是就我个人所知要记录返回的数据 ...
- 细说vue axios登录请求拦截器
当我们在做接口请求时,比如判断登录超时时候,通常是接口返回一个特定的错误码,那如果我们每个接口都去判断一个耗时耗力,这个时候我们可以用拦截器去进行统一的http请求拦截. 1.安装配置axios cn ...
- vue 路由拦截器和请求拦截器
路由拦截器 已路由为导向 router.beforeEach((to,from,next)=>{ if(to.path=='/login' || localStorage.getItem('to ...
随机推荐
- 企业微信域名IP列表
https://res.mail.qq.com/zh_CN/wework_ip/latest.html?st=C98F886B96A94AD2207D9F0B2970B93DFD5A76DF94CED ...
- matlab学习笔记9 高级绘图命令_1 图形对象_根对象,轴对象,用户控制对象,用户菜单对象
一起来学matlab-matlab学习笔记9 高级绘图命令_1 图形对象_根对象,轴对象,用户控制对象,用户菜单对象 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matl ...
- mysql or条件查询优化
当使用or的时候是不会用到索引的 explain SELECT * FROM aladdin_resource WHERE state = 1 OR state = 2; 解决办法就是用union替换 ...
- 测试从应用到DB的准确的网络延迟
最近几大云厂商把底层的基础设施包装起来后,通过Ping等很难知道真正的网络延迟了,因为通过ping还没到db都返回了,如何知道从应用到db的真正网络延迟呢? 用我的脚本 #!/bin/bash m= ...
- python 实现微信发送消息
背景:利用Python来登入你个人的手机微信,之后向朋友发送消息,发送的消息可以来源于网页.下面的例子就是取得当前日元的汇率,之后发送自己的某一个朋友的手机上 环境:Python3,JetBrains ...
- win运行canal
1.mysql开启log_bin 用命令查看是否开启 SHOW VARIABLES LIKE 'log_bin'; OFF为未开启,ON为开启 Win环境的话找到my.ini,Linux环境的话找到m ...
- QT QML 在qml中自定义信号
服从真理,就能征服一切事物. -- 塞涅卡 实例: 自定义文件 MoveYou.qml: import QtQuick 2.5 import QtQuick.Controls 1.4 import Q ...
- 数组转JSON对象
代码: function arrayToJson(arr){ var js={}; for(var i=0;i<arr.length;i++){ js[arr[i].name]=arr[i].v ...
- 玩转Spring全家桶笔记 02 那些好用的连接池HikariCP
1.前言 简单了解一下来自日本的一个新起之秀连接池---HikariCP github:https://github.com/brettwooldridge/HikariCP 2.HikariCP为什 ...
- c# 获取网页的爬虫程序
转载于:https://www.cnblogs.com/wzk153/p/9145684.html HtmlAgilityPack相关详解: https://www.cnblogs.com/asxin ...