Spring Cloud Feign如何进行统一设置呢?(设置统一请求头Header)呢?

新年 SpringBoot 发布时间:2024-02-10 22:28:55 阅读数:5031 1
下文笔者讲述Feign中设置统一请求头的方法及示例分享,如下所示

设置统一请求头的实现思路

feign调用时,有时需要设置统一的请求头,如添加调用token
      那么我们如何处理呢?此时我们只需编写一个统一配置类即可
============================================================
实现RequestInterceptor接口,作为配置类注入到Spring容器中即可
   如果这个实现类注入到Spring容器的话就会全局有效
      在没有指定configuration属性的FeignClient也会使用该配置。

    如果只想给指定FeignClient的feign接口使用该配置
      则无需将该类配置注入Spring中
       需要进行手动指定(假设实现类名为:TestFeignIntercepter):

@FeignClient(name = "user", configuration = {TestRequestInterceptor.class})
public interface UserFeignClient {
    @GetMapping(value = "/user/{id}")
    public User findById(@RequestParam("id") String id);
}

简单示例
public class TestFeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        String url = requestTemplate.url();
        // 只对特定url进行签名添加Header的操作
        if (!url.startsWith("/test/api")) {
            return;
        }
        
        requestTemplate.header("Authorization", "xxx");
    }
}
例1
Controller的所有header
import feign.RequestInterceptor;
import feign.RequestTemplate;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
public class FeignTransmitAllHeaderInterceptor implements RequestInterceptor {
    
    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                String values = request.getHeader(name);
                template.header(name, values);
            }
        }
    }
}
例2
   转发Controller的header(修改部分header)
import feign.RequestInterceptor;
import feign.RequestTemplate;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
public class FeignTransmitAllHeaderInterceptor implements RequestInterceptor {
 
    @Override
    public void apply(RequestTemplate requestTemplate) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            ServletRequestAttributes attributes = (ServletRequestAttributes)requestAttributes;
            HttpServletRequest request = attributes.getRequest();
            Enumeration<String> headerNames = request.getHeaderNames();
            if (headerNames != null) {
                while(headerNames.hasMoreElements()) {
                    String headerName; = (String)headerNames.nextElement();
                    String headerValue = request.getHeader(headerName);
                    if (!headerName.equals("content-length")) {
                        requestTemplate.header(headerName, headerValue);
                    }
                }
 
                String path = request.getContextPath();
                String modifiedReferer = request.getScheme() + "://" + request.getServerName() 
                         + ":" + request.getServerPort() + path + request.getServletPath();
                requestTemplate.header("referer", modifiedReferer);
            }
        }
    }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/SpringBoot/202402/7951.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者