如何为特定客户端设置自定义 Feign RequestInterceptor?
How to set a custom Feign RequestInterceptor for specific clients?
我需要为一些新的假客户端添加自定义授权 header。所以我写了一个 RequestInterceptor 并且它起作用了,但关键是我不希望这个自定义 RequestInterceptor 影响我的老客户。我尝试使用 template.url() 方法进行过滤,但它没有给我整个 url 请求,它只包含客户端方法 url (不是 url 和在客户端上方宣布的路径 class)。
我的问题是如何定位拦截器?
这是我的配置:
@Configuration
open class FeignCustomConfiguration {
private fun generateToken(): String { ... }
@Bean
open fun requestInterceptor(): RequestInterceptor {
return RequestInterceptor {
it.header("Authorization", generateToken())
}
}
}
我找到了解决方案。
对于每个 FeignClient 都有一个 configuration
选项,它接受一个 classes 数组。在 kotlin 中将 class 分配给配置的语法如下:
@FeignClient(
name = "feign-client",
path = "/path",
url = "https://example.com",
configuration = [FeignCustomConfiguration::class]
)
interface FeignCustomClient {
...
}
通过这个分配,每个 FeignClient 都有自己的配置,RequestInterceptor 不与其他客户端打交道。
我需要为一些新的假客户端添加自定义授权 header。所以我写了一个 RequestInterceptor 并且它起作用了,但关键是我不希望这个自定义 RequestInterceptor 影响我的老客户。我尝试使用 template.url() 方法进行过滤,但它没有给我整个 url 请求,它只包含客户端方法 url (不是 url 和在客户端上方宣布的路径 class)。 我的问题是如何定位拦截器?
这是我的配置:
@Configuration
open class FeignCustomConfiguration {
private fun generateToken(): String { ... }
@Bean
open fun requestInterceptor(): RequestInterceptor {
return RequestInterceptor {
it.header("Authorization", generateToken())
}
}
}
我找到了解决方案。
对于每个 FeignClient 都有一个 configuration
选项,它接受一个 classes 数组。在 kotlin 中将 class 分配给配置的语法如下:
@FeignClient(
name = "feign-client",
path = "/path",
url = "https://example.com",
configuration = [FeignCustomConfiguration::class]
)
interface FeignCustomClient {
...
}
通过这个分配,每个 FeignClient 都有自己的配置,RequestInterceptor 不与其他客户端打交道。