Feign接口调用添加请求头信息

1.方式一: @RequestHeader注解添加参数到请求头
//app-server-name:要调用的服务名
//appcontext:要调用的服务的上下文
@FeignClient(value="${app.feign.url:app-server-name/appcontext}")
public interface MyTestFeign {
    @GetMapping("/test")
    String test(@RequestParam("param1") String param1,
                 @RequestParam("param2") String param2,
                 @RequestParam("param3") String param3,
                 @RequestHeader(name = "Token",required = true) String token);
}
2.方式二: 实现RequestInterceptor接口: feign全局设置
package com.qin.feign.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;

/**
 * @author wzb
 * @version V1.0.0
 * @desciption feign请求全局添加Token
 * @date 2021/4/15 19:15
 * @className MyFeignRequestInterceptor
 */
@Configuration
public class MyFeignRequestInterceptor  implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        requestTemplate.header("Token", "your token value");
    }
}
//app-server-name:要调用的服务名
//appcontext:要调用的服务的上下文
@FeignClient(value="${app.feign.url:app-server-name/appcontext}",configuration = MyFeignRequestInterceptor.class)
public interface MyTestFeign {
    @GetMapping("/test")
    String test(@RequestParam("param1") String param1,
                 @RequestParam("param2") String param2,
                 @RequestParam("param3") String param3,
                 @RequestHeader(name = "Token",required = true) String token);
}
3.方式三: aop切面
3.1.注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

}
3.2.切面
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

public class MyFeignAspect {

    @Pointcut("@annotation(com.qin.annotation.MyAnnotation)")
    public void annotationPointCut() {

    }

    @Before("annotationPointCut()")
    public void doBefore() {
        //获取当前请求
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        HeaderMapRequestWrapper requestWrapper = new HeaderMapRequestWrapper(request);
        //设置请求头
        requestWrapper.setHeader("Token", "your token value");
    }

}
3.3.适配类
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.*;


public class HeaderMapRequestWrapper extends HttpServletRequestWrapper {
    private Map<String, String> headerMap = new HashMap<>();

    /**
     * Constructs a request object wrapping the given request.
     *
     * @param request the {@link HttpServletRequest} to be wrapped.
     * @throws IllegalArgumentException if the request is null
     */
    public HeaderMapRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    public void setHeader(String name, String value) {
        headerMap.put(name, value);
    }

    @Override
    public String getHeader(String name) {
        String headerValue = super.getHeader(name);
        if (headerMap.containsKey(name)) {
            headerValue = headerMap.get(name);
        }
        return headerValue;
    }

    @Override
    public Enumeration<String> getHeaderNames() {
        List<String> names = Collections.list(super.getHeaderNames());
        headerMap.keySet().forEach(names::add);
        return Collections.enumeration(names);
    }

    @Override
    public Enumeration<String> getHeaders(String name) {
        List<String> values = Collections.list(super.getHeaders(name));
        if (headerMap.containsKey(name)) {
            values = Collections.singletonList(headerMap.get(name));
        }
        return Collections.enumeration(values);
    }
}
3.4.接口
//app-server-name:要调用的服务名
//appcontext:要调用的服务的上下文
@FeignClient(value="${app.feign.url:app-server-name/appcontext}")
public interface MyTestFeign {
  
    @GetMapping("/test")
    @MyAnnotation
    String test(@RequestParam("param1") String param1,
                @RequestParam("param2") String param2,
                @RequestParam("param3") String param3);
}
  • 4
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值