首页 > 其他分享 >Gateway过滤器中调用OpenFeign时出现循环依赖问题

Gateway过滤器中调用OpenFeign时出现循环依赖问题

时间:2024-03-15 12:44:07浏览次数:18  
标签:return OpenFeign exchange request class token 过滤器 path Gateway

为了保证JWT随机生成的密钥一致,我设计了一个token服务,专门获取JWT,和生成token。在网关使用client调用服务时,出现了bean循环依赖

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  gateWayGlobalFilter defined in file [C:\Users\yinchrn\Desktop\Graduation project\mall-post\springcloud\gateway\target\classes\org\example\filter\GateWayGlobalFilter.class]
↑     ↓
|  org.example.client.TokenClient
↑     ↓
|  corsGatewayFilterApplicationListener defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]
↑     ↓
|  routePredicateHandlerMapping defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]
↑     ↓
|  filteringWebHandler defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]
└─────┘

通过在依赖注入时添加@Lazy解决

@Component
@Slf4j
public class GateWayGlobalFilter implements GlobalFilter, Ordered {
    private final AntPathMatcher antPathMatcher = new AntPathMatcher();
    @Autowired
    private AuthProperties authProperties;
    @Lazy
    @Autowired
    private TokenClient tokenClient;


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("GateWayGlobalFilter---------");
        ServerHttpRequest request = exchange.getRequest();
        List<String> excludePath = authProperties.getExcludePath();
        for (String s : excludePath) {
            System.out.println(s);
        }
        /*
            获取头文件验证实例,及拦截
         */
        if (isAllowPath(request)) {
            log.info("allowPath");
            return chain.filter(exchange);
        }
        /*
         * token验证
         * */
        String token;
        Long uid;
        List<String> strings = request.getHeaders().get("token");
        if (strings != null) {
            token = strings.get(0);
            log.info("token:{}",token);
            try{
                uid = tokenClient.getUid(new TokenDTO("0",0L,token)).getData();
                //更改传递给微服务的 header
            }catch (Exception e){
                log.error("token验证失败");
                log.error(e.getMessage());
                ServerHttpResponse response = exchange.getResponse();
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                return response.setComplete();
            }
        }else {
            log.error("token为空");
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }
        Long finalUid = uid;
        ServerWebExchange webExchange = exchange.mutate().request(
                        builder -> builder.header("uid", String.valueOf(finalUid)))
                        .build();
        return chain.filter(webExchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }

    public Boolean isAllowPath(ServerHttpRequest request){
//        HttpMethod method = request.getMethod();
        String path = request.getPath().toString();
        System.out.println(path);
        List<String> excludePathList = authProperties.getExcludePath();
        for (String excludePath : excludePathList) {
            if (antPathMatcher.match(excludePath,path)){
                return true;
            }
        }
        return false;
    }
}

https://github.com/spring-cloud/spring-cloud-openfeign/issues/142
https://github.com/progress0407/e-commerce-with-msa/issues/1

标签:return,OpenFeign,exchange,request,class,token,过滤器,path,Gateway
From: https://www.cnblogs.com/yinchrn/p/18075160

相关文章

  • 拦截器和过滤器(原理&区别)
    目录一、拦截器拦截器是什么拦截器的使用拦截器的实现导入依赖实现HandlerInterceptor接口注册拦截器拦截器的生命周期拦截器的执行顺序拦截器的生命周期多个拦截器的执行流程拦截器的实际使用拦截器实现日志记录实现接口幂等性校验拦截器的性能优化二、过滤器......
  • 第四章-OpenFeign 远程调用
    第四章SpringCloudOpenFeign在第二章中,我们通过RestTemplate实现了远程调用:@AutowiredprivateDiscoveryClientdiscoveryClient;privateStringgetLoadBalancedServerAddress(){List<ServiceInstance>instances=discoveryClient.getInstances("depart-......
  • 第五章-Gateway网关
    第五章GateWay网关在上一章结尾,我们拆分了微服务:商品服务8081购物车服务8082用户服务8083交易服务8084支付服务8085在最后进行前后端联调时,Nginx的配置:server{listen18080;server_namelocalhost;#指定前端项......
  • 分布式微服务 - 3.服务网关 - 4.Gateway
    分布式微服务-3.服务网关-4.Gateway项目示例:项目示例-3.服务网关-3.Gateway内容提要:基本使用:配置方式、代码方式内置断言、自定义断言内置局部过滤器、自定义内置和全局过滤器文档:官网官网文档基本使用配置方式引入依赖:使用gateway依赖时,不能同时引入sprin......
  • 项目示例 - 3.服务网关 - 3.Gateway
    项目示例-3.服务网关-3.Gateway关联知识:分布式微服务-3.服务网关-4.Gateway内容提要:基本使用自定义断言自定义局部、全局过滤器基本使用建Module:微服务起名为gateway-server改pom:引入以下依赖<dependencies><!--gateway--><dependency......
  • openfeign,webClient, restTemplate 忽略 ssl 证书
    0springboot版本<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.0.3</version><relativePath/><!--lookupparentfromr......
  • 异常过滤器
    ​一.ExceptionFilter作用将未处理的异常以特定的方式返回给前端二.ExceptionFilter使用1.实现IAsyncExceptionFilter接口(异步)2.实现IExceptionFilter接口(同步)2.1常用属性1.context.Exception代表异常信息对象2.设置context.ExceptionHandled=true;就不会在执行后......
  • 海量用户注册整合布隆过滤器实现用户名唯一功能
    布隆过滤器介绍布隆过滤器可以理解为一个固定大小的数组,数组的大小初始化时自定义,每个元素都占用1bit,每个元素都是0或者1,所以可以对海量的数据进行判断,原理图如图所示根据原理图可以得出信息,布隆过滤器说某个元素存在,小概率会误判。布隆过滤器说某个元素不在,那么这个元素一定......
  • 过滤器和拦截器的辨析
    过滤器和拦截器的辨析介绍过滤器和拦截器都是为了在请求到达目标处理器(Servlet或Controller)之前或者之后插入自定义的处理逻辑过滤器:遵循AOP(面向切面编程)思想实现,基于Servlet规范提供的Filter接口,它是位于客户端请求与服务器响应之间的一个组件,依赖于Servlet容器。当......
  • SpringCloud Gateway实战
    SpringCloudGateway目录SpringCloudGateway认识SpringCloudGatewaySpringCloudGateway和Zuul最核心的区别SpringCloudGateway工作模型图及解读SpringCloudGateway三大核心概念Route、Predicate、FilterSpringCloudGateway过滤器全局过滤器和局部过滤器SpringCloudGatew......