SpringCloud OpenFeign遠(yuǎn)程調(diào)用傳遞請(qǐng)求頭信息方式
更新時(shí)間:2026年03月19日 08:45:48 作者:lingering fear
這篇文章主要介紹了SpringCloud OpenFeign遠(yuǎn)程調(diào)用傳遞請(qǐng)求頭信息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
1.實(shí)現(xiàn)RequestInterceptor接口
package com.liuxs.common.core.config;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
/**
* @author: Liu Yuehui
* @ClassName: RequestInterceptor
* @date: 2022/12/16 19:01
* @description: Feign請(qǐng)求攔截器(設(shè)置請(qǐng)求頭,傳遞登錄信息)
**/
@Slf4j
public class FeignBasicAuthRequestInterceptor implements RequestInterceptor{
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
Assert.notNull(attributes , "FeignBasicAuthRequestInterceptor apply() attributes is null");
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);
}
}
Enumeration<String> bodyNames = request.getParameterNames();
StringBuilder body = new StringBuilder();
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);
requestTemplate.body(body.toString());
log.info("feign interceptor body:{}" , body.toString());
}
}
}
2.注冊(cè)配置
package com.liuxs.gateway.config;
import com.liuxs.common.core.config.FeignBasicAuthRequestInterceptor;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author: Liu Yuehui
* @ClassName: FeignSupportConfig
* @date: 2022/12/16 19:21
* @description: Feign配置注冊(cè)(全局)
**/
@Configuration
public class FeignSupportConfig {
@Bean
public RequestInterceptor requestInterceptor() {
return new FeignBasicAuthRequestInterceptor();
}
}
我在工程中是這樣使用的:


總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
Spring使用@Value注解與@PropertySource注解加載配置文件操作
這篇文章主要介紹了Spring使用@Value注解與@PropertySource注解加載配置文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringBoot+Vue實(shí)現(xiàn)動(dòng)態(tài)菜單的思路梳理
這篇文章主要為大家詳細(xì)介紹了利用SpringBoot+Vue實(shí)現(xiàn)動(dòng)態(tài)菜單的思路梳理,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-07-07
SpringBoot整合Shiro靜態(tài)資源攔截配置實(shí)踐
本文介紹了Spring?Boot整合Thymeleaf訪問(wèn)resources目錄下static靜態(tài)資源的方法,并提供三種解決方式,方法一通過(guò)修改訪問(wèn)路徑,方法二在yml文件中修改配置,方法三通過(guò)創(chuàng)建statics目錄并調(diào)整攔截配置來(lái)實(shí)現(xiàn)2026-05-05
使用JPA自定義VO類型轉(zhuǎn)換(EntityUtils工具類)
這篇文章主要介紹了使用JPA自定義VO類型轉(zhuǎn)換(EntityUtils工具類),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot 并發(fā)登錄人數(shù)控制的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot 并發(fā)登錄人數(shù)控制的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Springboot?注解EqualsAndHashCode詳解
注解@EqualsAndHashCode主要用于自動(dòng)生成equals方法和hashCode方法,callSuper屬性為true時(shí),生成的方法會(huì)包括父類字段,為false則只包含當(dāng)前類字段,IDEA工具中有檢查提示并可自動(dòng)修復(fù)相關(guān)代碼,確保注解正確使用,更多詳解可查閱相關(guān)文檔2024-10-10
Java中Collection與Collections的區(qū)別詳解
這篇文章主要為大家詳細(xì)介紹了Java中Collection與Collections的區(qū)別,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,感興趣的同學(xué)可以參考一下2023-06-06

