我有许多客户端已经为其定义了“全局”RequestInterceptor。对于其中一个客户端,我需要排除这个“全局”拦截器。是否可以覆盖特定FeignClient的全套RequestInterceptors?
@FeignClient(value = "foo", configuration = FooClientConfig.class)
public interface FooClient {
//operations
}
@Configuration
public class FooClientConfig{
//How do I exclude global interceptors from this client configuration?
}使用的spring-cloud-netflix版本是1.1.0 M5
发布于 2018-05-04 13:22:31
似乎没有简单的方法来覆盖全局拦截器。我认为你可以这样做:
@Configuration
public class FooClientConfig{
@Bean
RequestInterceptor globalRequestInterceptor() {
return template -> {
if (template.url().equals("/your_specific_url")) {
//don't add global header for the specific url
return;
}
//add header for the rest of requests
template.header(AUTHORIZATION, String.format("Bearer %s", token));
};
}
}发布于 2020-03-03 22:46:54
解决此问题的一种增强方法是将自定义头部传递给您的请求,如下所示:
@PostMapping("post-path")
ResponseEntity<Void> postRequest(@RequestHeader(HEADER_CLIENT_NAME) String feignClientName, @RequestBody RequestBody requestBody);我只想在拦截器中为这个假客户端设置头。在设置标头之前,拦截器首先检查HEADER_CLIENT_NAME标头是否存在并具有所需的值:
private boolean criteriaMatches(RequestTemplate requestTemplate) {
Map<String, Collection<String>> headers = requestTemplate.headers();
return headers.containsKey(HEADER_CLIENT_NAME)
&& headers.get(HEADER_CLIENT_NAME).contains("feign-client-name");
}因此,您可以在设置基本身份验证之前进行检查。在拦截器中:
@Override
public void apply(RequestTemplate template) {
if (criteriaMatches(template)) {
/*apply auth header*/
}
}这样,其他假客户端的请求就不会被这个拦截器操纵了。
最后,我将feignClientName设置为请求:
feignClient.postRequest("feign-client-name", postBody);https://stackoverflow.com/questions/36018879
复制相似问题