Spring Cloud Gateway 全局过滤器 Global Filters

 

局部过滤器和全局过滤器的区别:

1、局部过滤器针对某个路由,需要在路由中进行配置

2、全局过滤器针对所有请求,一但定义就会投入使用

https://docs.spring.io/spring-cloud-gateway/docs/2.2.6.RELEASE/reference/html/#global-filters

 

 

自定义全局过滤器

 

package com.wsm.filters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class LogFilter implements GlobalFilter {

    Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("===log======="+exchange.getRequest().getPath().value());
        return chain.filter(exchange);
    }

}

 

 

 

 

posted @ 2022-01-20 20:55  残星  阅读(265)  评论(0编辑  收藏  举报