Spring boot 如何在Spring Boot外部客户端上定义全局静态头
我有一个spring boot应用程序,希望创建一个具有静态定义的头值(用于auth,但不用于基本auth)的外部客户端。我找到了Spring boot 如何在Spring Boot外部客户端上定义全局静态头,spring-boot,feign,Spring Boot,Feign,我有一个spring boot应用程序,希望创建一个具有静态定义的头值(用于auth,但不用于基本auth)的外部客户端。我找到了@Headers注释,但它在Spring Boot领域似乎不起作用。我怀疑这与使用SpringMvcContract有关 以下是我想使用的代码: @FeignClient(name = "foo", url = "http://localhost:4444/feign") @Headers({"myHeader:value"}) public interface Lo
@Headers
注释,但它在Spring Boot领域似乎不起作用。我怀疑这与使用SpringMvcContract
有关
以下是我想使用的代码:
@FeignClient(name = "foo", url = "http://localhost:4444/feign")
@Headers({"myHeader:value"})
public interface LocalhostClient {
但它不添加标题
我用我的尝试制作了一个干净的spring启动应用程序,并发布到github:
我能够使它工作的唯一方法是将
RequestInterceptor
定义为一个全局bean,但我不想这样做,因为它会影响其他客户端。您可以在您的外部接口上设置一个特定的配置类,并在其中定义RequestInterceptor bean。例如:
@FeignClient(name = "foo", url = "http://localhost:4444/feign",
configuration = FeignConfiguration.class)
public interface LocalhostClient {
}
@Configuration
public class FeignConfiguration {
@Bean
public RequestInterceptor requestTokenBearerInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate requestTemplate) {
// Do what you want to do
}
};
}
}
您还可以通过向各个方法添加标题来实现这一点,如下所示:
@RequestMapping(method = RequestMethod.GET, path = "/resource", headers = {"myHeader=value"})
讨论使用
@RequestHeader
的动态值解决方案,谢谢!我对@Configuration方法唯一关心的是,Feign需要该注释,您必须小心将其排除在组件扫描之外。这对我来说似乎很危险。如果这个类以某种方式被添加到组件扫描中,我们会突然将敏感信息发送到所有外部端点+1尽管如此,因为这从技术上回答了我的具体问题,尽管我将使用阿里的建议