如何使用Feign传递请求头信息
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章将为大家详细讲解有关如何使用Feign传递请求头信息,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。Feign传递请求头信息在我之前的文章服务网关Spring
千家信息网最后更新 2026年11月03日如何使用Feign传递请求头信息
这篇文章将为大家详细讲解有关如何使用Feign传递请求头信息,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
Feign传递请求头信息
在我之前的文章服务网关Spring Cloud Zuul中,将用户的登录id放在了请求头中传递给内部服务。
但是当内部服务之间存在feign调用时,那么请求头信息会在feign请求的时候传递吗?不会,请求的头信息和请求参数都不会进行传递。
但是我们可以通过通过实现RequestInterceptor接口,完成对所有的Feign请求,传递请求头和请求参数。
实现RequestInterceptor接口
import feign.RequestInterceptor;import feign.RequestTemplate;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 simon * @create 2018-09-07 9:51 **/public class FeignBasicAuthRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); Enumeration headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String values = request.getHeader(name); requestTemplate.header(name, values); } } }} 这里只设置了请求头,如果想传递请求参数,可以参考如下代码:
public class FeignBasicAuthRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); Enumeration headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String values = request.getHeader(name); requestTemplate.header(name, values); } } Enumeration bodyNames = request.getParameterNames(); StringBuffer body =new StringBuffer(); if (bodyNames != null) { while (bodyNames.hasMoreElements()) { String name = bodyNames.nextElement(); String values = request.getParameter(name); body.append(name).append("=").append(values).append("&"); } } if(body.length()!=0) { body.deleteCharAt(body.length()-1); template.body(body.toString()); logger.info("feign interceptor body:{}",body.toString()); } }} 注册配置
package com.southgis.ibase.personalConfigure.config;import com.southgis.ibase.utils.FeignBasicAuthRequestInterceptor;import com.southgis.ibase.utils.FeignSpringFormEncoder;import feign.RequestInterceptor;import feign.codec.Encoder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * Feign配置注册(全局) * * @author simon * @create 2018-08-20 11:44 **/@Configurationpublic class FeignSupportConfig { /** * feign请求拦截器 * * @return */ @Bean public RequestInterceptor requestInterceptor(){ return new FeignBasicAuthRequestInterceptor(); }}这个文件放在项目的扫描目录下,所有的feign调用都会使用此配置。如果只有某个feign调用则可以这样设置(但配置类不能在扫描目录下):
@FeignClient(name = "organ",path = "/organ/OrganInfo",configuration = FeignSupportConfig.class)
Feign调用微服务传递header请求头
package com.chitic.module.core.config;import feign.RequestInterceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;import java.util.Enumeration; @Configurationpublic class FeignConfig { @Bean public RequestInterceptor headerInterceptor() { return template -> { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (null != attributes) { HttpServletRequest request = attributes.getRequest(); Enumeration headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String values = request.getHeader(name); template.header(name, values); } } } }; }} 需注意,feign调用时不能调用含有HttpServletResponse参数(比如常用的数据导出),以下就不能远程调用,目前没找到解决办法

关于"如何使用Feign传递请求头信息"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
信息
参数
服务
配置
篇文章
接口
文章
更多
目录
拦截器
参考
登录
不错
实用
之间
代码
全局
内容
办法
只有
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
小米移动互联网科技园
互联网科技行业发展
中国 科技 互联网
互联网 卓越科技
唐信互联网科技
互联网科技论文
腾讯科技 互联网女皇
互联网科技视频素材
联恩商钥互联网科技有限公司
成都仁贤领冠互联网科技有限公司
深圳互联网科技怎么样
中化能源互联网科技招聘
南山科技园互联网公司有哪些
互联网科技创新的路径
为客找房互联网科技公司怎么样
中天科技中国工业互联网
萌小萌互联网科技河南有限公司
科技互联网经济论坛
华人运通互联网科技公司
重庆科技产业互联网有哪些
什么是科技互联网
上海卓越科技互联网
互联网 科技
高科技与互联网
上海致骋互联网科技
汉立互联网科技有限公司联系电话
湖南诚度互联网科技有限公司
互联网科技公司有风险吗
注册互联网科技公司经营范围大全
如何创新互联网科技