当前位置: 首页 > news >正文

feign的全局拦截器和局部拦截器

拦截器和过滤器在java中用的最多的应该是处理一些head头,进行鉴权,签名等处理。feign也不例外。

一、feign全局拦截器

全局拦截器对每个feign客户端都生效

配置这里省略。

全局拦截器定义

package com.vvvtimes.interceptor;import feign.RequestInterceptor;
import feign.RequestTemplate;// 全局拦截器类
public class GlobalFeignRequestInterceptor implements RequestInterceptor {@Overridepublic void apply(RequestTemplate template) {// 这里可以添加全局的请求头或其他公共参数System.out.println("调用全局拦截器");}}

拦截器配置类

package com.vvvtimes.config;import com.vvvtimes.interceptor.GlobalFeignRequestInterceptor;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class FeignGlobalInterceptorConfig {@Beanpublic RequestInterceptor globalInterceptor() {return new GlobalFeignRequestInterceptor();}}

feign客户端配置

package com.vvvtimes.client;import com.vvvtimes.config.FeignCustomInterceptorConfiguration;
import com.vvvtimes.interceptor.LocalFeignRequestInterceptor;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;//注意,只有取消全局的@Configuration注解才能使用局部拦截器配置
//如果全局和局部同时配置 先走局部拦截器,后走全局拦截器
//@FeignClient(name = "provider",configuration = LocalFeignRequestInterceptor.class)
//@FeignClient(name = "provider",configuration = FeignCustomInterceptorConfiguration.class)
@FeignClient(name = "provider")
public interface HelloClient {@GetMapping("/hello")String hello();
}

配置完成后,调用所有feign客户端的代码会先走apply方法里的逻辑,我们可以把签名逻辑放这里,所有feign服务如果签名一样还好,如果不一样,可以在feign客户端传入自定义请求头区分。不过最好还是使用局部拦截器

二、局部拦截器

如果之前写了全局拦截器,那么去掉@Configuration注解,否则会两个拦截器都执行一遍。

局部拦截器类定义

package com.vvvtimes.interceptor;import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;// 局部拦截器类
public class LocalFeignRequestInterceptor implements RequestInterceptor {@Overridepublic void apply(RequestTemplate template) {// 这里可以添加特定于这个 Feign 客户端的请求头或其他参数System.out.println("调用局部拦截器");}}

局部拦截器配置类

package com.vvvtimes.config;import com.vvvtimes.interceptor.LocalFeignRequestInterceptor;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;//@Configuration
public class FeignCustomInterceptorConfiguration {@Beanpublic RequestInterceptor localInterceptor() {return new LocalFeignRequestInterceptor();}
}

feign客户端配置

package com.vvvtimes.client;import com.vvvtimes.config.FeignCustomInterceptorConfiguration;
import com.vvvtimes.interceptor.LocalFeignRequestInterceptor;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;//注意,只有取消全局的@Configuration注解才能使用局部拦截器配置
//如果全局和局部同时配置 先走局部拦截器,后走全局拦截器
//@FeignClient(name = "provider",configuration = LocalFeignRequestInterceptor.class)
@FeignClient(name = "provider",configuration = FeignCustomInterceptorConfiguration.class)
public interface HelloClient {@GetMapping("/hello")String hello();
}

这里的局部拦截器的FeignClient 可以指定拦截器配置类也可以指定为拦截器类。

三、同时使用全局拦截器和局部拦截器会有什么效果

经过验证,拦截器都会生效,先走局部拦截器,后走全局拦截器。

本文完整项目代码如下。

https://download.csdn.net/download/gsls200808/89807735


http://www.mrgr.cn/news/38192.html

相关文章:

  • 为啥我关不掉页面?Chrome扩展开发中的那些坑
  • LLM大模型学习:致AI新手掌握这些经验,助你少走三年弯路!
  • 纠删码参数自适应匹配问题ECP-AMP实验方案(一)
  • Java实现找色和找图功能
  • 股票实时行情接口,私募量化软件的交易核心是什么
  • 代码为笔,合作作墨,共绘共赢画卷———未来之窗行业应用跨平台架构
  • 笔记本维修与拆解(一)
  • 『网络游戏』GoLand服务器框架【01】
  • 相机、镜头参数详解以及相关计算公式
  • SigLIP技术小结
  • 安全点的应用场景及其原理详解
  • 2条件欧几里得聚类
  • c++简体中文与繁体中文互转
  • 智尚招聘求职小程序V1.0.17
  • 【算法】字符串相关
  • 【Linux】Linux环境基础开发工具使用
  • 小徐影院:探索Spring Boot的影院管理
  • python开发讯飞星火
  • 设计模式-策略模式-200
  • 华为 HCIP-Datacom H12-821 题库 (29)