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调用没有请求头
发布评论