1. public class FeignAuthRequestInterceptor implements RequestInterceptor {
    2. @Override
    3. public void apply(RequestTemplate template) {
    4. // 业务逻辑
    5. String access_token = UUID.randomUUID().toString();
    6. template.header("Authorization",access_token);
    7. }
    8. }
    9. @Configuration // 全局配置
    10. public class FeignConfig {
    11. @Bean
    12. public Logger.Level feignLoggerLevel() {
    13. return Logger.Level.FULL;
    14. }
    15. /**
    16. * 自定义拦截器
    17. * @return
    18. */
    19. @Bean
    20. public FeignAuthRequestInterceptor feignAuthRequestInterceptor(){
    21. return new FeignAuthRequestInterceptor();
    22. }
    23. }

    测试
    image.png
    补充:可以在yml中配置

    1. feign:
    2. client:
    3. config:
    4. mall-order: #对应微服务
    5. requestInterceptors[0]: #配置拦截器
    6. com.tuling.mall.feigndemo.interceptor.FeignAuthRequestInterceptor

    mall-order端可以通过 @RequestHeader获取请求参数
    建议在filter,interceptor中处理