public class FeignAuthRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
// 业务逻辑
String access_token = UUID.randomUUID().toString();
template.header("Authorization",access_token);
}
}
@Configuration // 全局配置
public class FeignConfig {
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
/**
* 自定义拦截器
* @return
*/
@Bean
public FeignAuthRequestInterceptor feignAuthRequestInterceptor(){
return new FeignAuthRequestInterceptor();
}
}
测试
补充:可以在yml中配置
feign:
client:
config:
mall-order: #对应微服务
requestInterceptors[0]: #配置拦截器
com.tuling.mall.feigndemo.interceptor.FeignAuthRequestInterceptor
mall-order端可以通过 @RequestHeader获取请求参数
建议在filter,interceptor中处理