去除 RequestTemplate 对象中的指定请求头
目录
- 目标
- 实现
- 获取 RequestTemplate 对象
- 去除请求头
目标
去除 RequestTemplate 对象中的指定请求头,如 Authorization 等。
实现
获取 RequestTemplate 对象
获取 RequestTemplate 对象的方式有很多种,如 通过 feign 虚拟客户端配置器:
@FeignClient(url = “https://xxx.com”, name = “XxxService”, configuration = XxxFeignConfiguration.class)
可以在配置器中获取 RequestTemplate 对象
@Slf4j
@Import(TokenInterceptorConfiguration.class)
public class SapFeignConfiguration {
@Bean
public RequestInterceptor tokenHeaderInterceptor() {
return requestTemplate -> {
};
}
}
RequestTemplate 对象信息:
去除请求头
获取到 RequestTemplate 对象后,可以根据方法 removeHeader 去除指定请求头
feign.RequestTemplate#removeHeader
具体方法:
private static final String AUTHORIZATION_HEADER = "Authorization";
Collection<String> authorizationHeaders = headers.get(AUTHORIZATION_HEADER);
if (!CollectionUtils.isEmpty(authorizationHeaders)) {
requestTemplate.removeHeader(AUTHORIZATION_HEADER);
}
去除 Authorization 前:
去除 Authorization 后: