实现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 ...
随机推荐
- Robot Framwork关键字驱动+RedwoodHQ安装
一.Robot Framwork介绍 Robot Framwork是一款python编写的功能框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式测试执行. ...
- 一篇文章学会shell脚本
一.Shell传递参数 #!/bin/bash # 假设在脚本运行时写了三个参数 ..,,则 "(传递了三个参数). echo "-- \$* 演示 --" for i ...
- python 传入任意多个参数(方法调用可传参或不传参)
1.可传参数与不传参数,在定义中给参数设置默认值 class HandleYmal: """ 获取测试环境的配置 """ def __ini ...
- ingress Whitelisting白名单机制
Whitelisting To restrict the service in a way that only a list of IPs can access it, modify the ingr ...
- python测试工具nosetests
今天在github上找东西,找到个工具是python写的,但是需要安装nosetests,因此了解了下nosetests python除了unittest,还有nosetests,使用更快捷 nose ...
- sql 表的连接 inner join、full join、left join、right join、natural join
一.内连接-inner jion : SELECT * FROM table1 INNER JOIN table2 ON table1.field1 compopr table2.field2 INN ...
- 使用objcopy实现将文件编译进执行程序
一.简介 工作中可能遇到将一个文件编译进执行程序的需求,例如bin文件.jpg文件等等.实现的方法可以使用脚本来将文件内容写入一个新的C源文件数组,达成编译进程序的目的. 现在介绍一种简单.快捷的方 ...
- Git 工作流之 GitFlow
GitFlow学习: 先学习这篇:点击打开链接 Gitflow工作流是经典模型,体现了工作流的经验和精髓.随着项目过程复杂化,会感受到这个工作流中深思熟虑和威力. ////////////////// ...
- QT 安装 配置过程
QT, QT creator的安装,环境配置: 需要根据这个连接重新梳理一遍:https://blog.csdn.net/win_turn/article/details/50465127 1)一种方 ...
- Vue框架 03
Vue项目开发: 前后端完全分离 后端:提供接口数据 前端:页面转跳.页面布局.页面数据渲染全部由前端做 中间交互:请求 搭建Vue项目环境: Vue项目需要自建服务器:node node介绍: 1. ...