Feign如何在调用时添加token请求头呢?
下文笔者讲述Feign调用时,添加请求头的方法分享,如下所示
由于一些请求中,我们需加入请求头信息 那么Feign调用时,如何加入请求头信息呢?下文笔者将一一道来,如下所示
@RequestHeader注解参数添加到请求头的示例
@FeignClient(name = “capability-register”, fallback = ApiServiceClientFallBack.class ) public interface ApiServiceClient { @GetMapping("/api/fegin") Result test(@RequestParam("url") String path, @RequestParam("param") String param, @RequestParam("method") String method, @RequestParam("appKey") String appKey, @RequestHeader(name = "Token",required = true) String Token); }
feign调用时--自动设置请求头
package com.java265.provider.configuration; import feign.RequestInterceptor; import feign.RequestTemplate; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; /** Feign调用的时候添加请求头Token */ @Configuration public class FeignConfiguration implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); requestTemplate.header(“Token”, request.getHeader(“Token”)); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。