实现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 ...
随机推荐
- hive中同源多重insert写法
多重insert: with tmp_a as ( select name from tmp_test3 ) from tmp_a insert overwrite table tmp_test1 s ...
- 软件定义网络基础---REST API的设计规范
一:REST API的设计 REST API是基于HTTP协议进行设计的,由HTTP动词+URI组成 (一)HTTP动词 (二)资源的原型 文档(Document): 文档是资源的单一表现形式: 集合 ...
- DateUtil(2)
import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; impor ...
- nodejs 读取目前下所有文件
var fs = require('fs'); var join = require('path').join; function getJsonFiles(jsonPath) { let jsonF ...
- android RelativeLayout实现左中右布局
RelativeLayout实现左中右布局 <RelativeLayout android:layout_width="match_parent" android:lay ...
- [LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- Kubernetes Pod应用的滚动更新(八)
一.环境准备 我们紧接上一节的环境,进行下面的操作,如果不清楚的,可以先查看上一篇博文. 滚动更新是一次只更新一小部分副本,成功后,再更新更多的副本,最终完成所有副本的更新.滚动更新的最大的好处是零停 ...
- OpenJudge 4120 硬币
总时间限制: 1000ms 内存限制: 262144kB 描述 宇航员Bob有一天来到火星上,他有收集硬币的习惯.于是他将火星上所有面值的硬币都收集起来了,一共有n种,每种只有一个:面值分别为a1,a ...
- Python界面常用GUI包
作为Pyhon开发者,你迟早都会碰到图形用户界面(GUI)应用开发任务,这时候我们就需要一些界面库来帮助我们快速搭建界面,python的界面库很多,我认识的并不多,这里只列出几种我认识的 1.tkin ...
- spring中Bean的懒加载
在ApplicationContext容器中,当容器一启动时,所有的bean(单例的)都会被创建和注入依赖,这也被视为IOC容器启动过程中的一个步骤. 那如何让一个bean在需要的时候再被创建,而不是 ...