实现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 ...
随机推荐
- SpringMVC @SessionAttribute 使用说明
百度搜索 @SessionAttribute 这一句绝大多数文章中不存在: 如果Model中没有name参数,而session中存在一个name参数,那么SessionAttribute会讲这个参数塞 ...
- node端代理浏览器路由 解决浏览器跨域问题
var _ = require('lodash'); var request = require("request"); /* @LM 2017-02-16 node端代理浏览器路 ...
- [LeetCode] 61. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- Java程序运行机制
Java程序运行机制 编译型(compile) 它有一个负责翻译的程序(编译器),将我们写的 Java 源代码转为计算机可执行的代码 举个例子:把一本中文书翻译成英文书 应用:操作系统.C.C++ 解 ...
- PyCharm的安装方法及设置中文界面
pycharm官网下载安装包:https://www.jetbrains.com/pycharm/download/#section=windows 下载中文语言包:https://github.co ...
- 【剑指offer】面试题 23. 链表中环的入口节点
面试题 23. 链表中环的入口节点
- Spring中的AOP实现思路
AOP是面向切面编程,为什么在切面中写一个注解方法@Before,这个方法会在目标方法前面执行呢 基于JDK动态代理实现上面说的情况 自定义注解 @Target({ ElementType.METHO ...
- Integer的parseInt和valueOf的区别
先来看一下下面这段代码 String s = "1"; System.out.println(Integer.valueOf(s)); System.out.println(Int ...
- 基于全志a33-vstar开发板的ap6210WiFi模块移植
可以去链接看更详细的,第一次用博客,这个编辑方式太不友好了. 文档:全志a33--系统移植--ap6210WiFi模块移?..链接:http://note.youdao.com/noteshare?i ...
- mysql网文收录
1.分布式事务 1) 聊聊分布式事务,再说说解决方案 https://www.cnblogs.com/savorboard/p/distributed-system-transaction-cons ...