SpringCloud OpenFeign远程调用传递请求头信息方式
更新时间:2026年03月19日 08:45:48 作者:lingering fear
这篇文章主要介绍了SpringCloud OpenFeign远程调用传递请求头信息方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
1.实现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请求拦截器(设置请求头,传递登录信息)
**/
@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.注册配置
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配置注册(全局)
**/
@Configuration
public class FeignSupportConfig {
@Bean
public RequestInterceptor requestInterceptor() {
return new FeignBasicAuthRequestInterceptor();
}
}
我在工程中是这样使用的:


总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Spring使用@Value注解与@PropertySource注解加载配置文件操作
这篇文章主要介绍了Spring使用@Value注解与@PropertySource注解加载配置文件操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-06-06
使用JPA自定义VO类型转换(EntityUtils工具类)
这篇文章主要介绍了使用JPA自定义VO类型转换(EntityUtils工具类),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-11-11
Springboot 注解EqualsAndHashCode详解
注解@EqualsAndHashCode主要用于自动生成equals方法和hashCode方法,callSuper属性为true时,生成的方法会包括父类字段,为false则只包含当前类字段,IDEA工具中有检查提示并可自动修复相关代码,确保注解正确使用,更多详解可查阅相关文档2024-10-10
Java中Collection与Collections的区别详解
这篇文章主要为大家详细介绍了Java中Collection与Collections的区别,文中有详细的代码示例,具有一定的参考价值,感兴趣的同学可以参考一下2023-06-06


最新评论