feign调用没有请求头

编程入门 行业动态 更新时间:2024-10-05 19:11:22

<a href=https://www.elefans.com/category/jswz/34/1768189.html style=feign调用没有请求头"/>

feign调用没有请求头

feign增强请求头

1、自定义类FeignRequestInterceptor implements RequestInterceptor

package common.config;import cn.hutool.core.util.StrUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
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;/*** Copyright © 2021 FL All rights reserved.*/
@Slf4j
@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {@Overridepublic void apply(RequestTemplate template) {log.info("-----增强配置Feign请求头,开始------->");ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if (null != attributes) {HttpServletRequest request = attributes.getRequest();//获取到请求头名字Enumeration<String> headerNames = request.getHeaderNames();while (headerNames.hasMoreElements()) {String name = headerNames.nextElement();log.info("-----请求头的配置:name{}------->",name);if(!StrUtil.containsAny(name,"Authorization","applicationcode")){continue;}//获取到请求头String value = request.getHeader(name);log.info("-----请求头的配置:value{}------->",value);//经过处理逻辑后//使用进入的request对象来设置Feign请求对象的请求头template.header(name, value);}}log.info("-----增强配置Feign请求头,结束------->");}}

2、将拦截器添加到spring中

package com.scmmon.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;/*** Copyright © 2021 FL All rights reserved.* 描述*/
@Configuration
public class FeignRequestConfiguration{@Autowiredpublic FeignRequestInterceptor feignRequestInterceptor(){return new FeignRequestInterceptor();}}

更多推荐

feign调用没有请求头

本文发布于:2024-02-28 04:05:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1768156.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:feign

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!