feign拦截器

Feign拦截器

一般运用在feign调用前需要做什么操作, 在这里是在reques头中添加jwt

配置文件

  1. pom.xml

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    

拦截器

  1. FeignClientInterceptor

    package com.xuecheng.framework.interceptor;
    
    import feign.RequestInterceptor;
    import feign.RequestTemplate;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.Enumeration;
    
    /**
     * @Author: 潇哥
     * @DateTime: 2021/1/20 下午8:01
     * @Description: TODO
     */
    public class FeignClientInterceptor implements RequestInterceptor {
    
        /**
         * feign 拦截器, 因为feign远程调用的接口需要JWT, 拦截把JWT添加进去
         * @param template
         */
        @Override
        public void apply(RequestTemplate template) {
            try {
                //使用RequestContextHolder工具获取request相关变量
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    
                if (attributes != null) {
                    // 获取request
                    HttpServletRequest request = attributes.getRequest();
    
                    // 获取request中所有的头名称
                    Enumeration<String> headerNames = request.getHeaderNames();
    
                    if (headerNames != null) {
                        // 枚举是否包含更多的元素
                        while (headerNames.hasMoreElements()) {
                            // 获取头名称
                            String name = headerNames.nextElement();
                            String value = request.getHeader(name);
    
                            if (name.equals("authorization")) {
                                // 把令牌添加进去
                                template.header(name, value);
                            }
    
                        }
                    }
    
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }
    
    

启动类----(这里举得个例子, 那个微服务需要用feign拦截器, 就注入到那个微服务的启动类中)

  1. ManageCourseApplication

    package com.xuecheng.manage_course;
    
    import com.xuecheng.framework.interceptor.FeignClientInterceptor;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.domain.EntityScan;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    
    /**
     * @author Administrator
     * @version 1.0
     **/
    @EnableFeignClients // feign
    // 一个EurekaClient从EurekaServer发现服务
    @EnableDiscoveryClient
    @SpringBootApplication
    @EntityScan("com.xuecheng.framework.domain.course")//扫描实体类
    @ComponentScan(basePackages={"com.xuecheng.api"})//扫描接口
    @ComponentScan(basePackages={"com.xuecheng.manage_course"})
    @ComponentScan(basePackages={"com.xuecheng.framework"})//扫描common下的所有类
    public class ManageCourseApplication {
        public static void main(String[] args) throws Exception {
            SpringApplication.run(ManageCourseApplication.class, args);
        }
    
        @Bean
        public FeignClientInterceptor feignClientInterceptor(){
            return new FeignClientInterceptor();
        }
    }
    
  2. 远程调用报错看client接口就行
    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只因为你温柔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值