如何使用Feign传递请求头信息
发表于:2025-11-13 作者:千家信息网编辑
千家信息网最后更新 2025年11月13日,这篇文章将为大家详细讲解有关如何使用Feign传递请求头信息,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。Feign传递请求头信息在我之前的文章服务网关Spring
千家信息网最后更新 2025年11月13日如何使用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安全错误
数据库的锁怎样保障安全
栖霞平台软件开发
汝州市云端网络技术工作室
软件开发流程的方法jad
网络安全教育手抄报内容文字小学
平台数据库核查的通知
java手机版服务器是别人的吗
上海点礼网络技术有限公司
妇联网络安全宣传活动的总结
网络安全知识手册 新华网
如何购买阿里云的服务器并配置
阿里云服务器可以做嵌入式开发吗
美杜莎传奇服务器端
企业网络安全应用
中山市京几软件开发有限公司
服务器安全狗端口策略
网络技术公司的企业想法描述
盛世十月软件开发有限公司
如何取消服务器同步
广西学校数显钟服务器
深圳蜜蜂互联网科技
中博网络技术公司
2021年软件开发报价
服务器要清灰尘吗
嘉兴捷飞软件开发有限公司
数据库是先查重还是先合并
配电自动化网络安全培训
网络技术和数据库三级
数据库分页查询排序
最新的ibm服务器
华为社招5g软件开发面试