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

为了保证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

posted @ 2024-03-15 12:37  钟音城  阅读(778)  评论(0)    收藏  举报