Spring Cloud Gateway: Global Filters

The GlobalFilter interface has the same signature as GatewayFilter. These are special filters that are conditionally applied to all routes.

Note: This interface and how it is used may change in future milestone releases.

1. Combination sorting of global filter and gateway filter

When a request matches a route, the filter's web handler adds all GlobalFilter instances and route-specific GatewayFilter instances to the filter chain. The combined filter chain will be sorted according to the org.springframework.core.Ordered interface, and you can set the sort order by implementing the getOrder() method.

Since Spring Cloud Gateway distinguishes between the "pre" phase and the "post" phase when the filter logic is executed (see "Working Principles" for details), the filter with the highest priority will be the first filter in the "pre" phase and the " post" stage as the last filter.

The following example configures a filter chain:

ExampleConfiguration.java

@Bean
public GlobalFilter customFilter() {
    return new CustomGlobalFilter();
}

public class CustomGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("custom global filter");
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -1;
    }

Guess you like

Origin blog.csdn.net/qq_29901385/article/details/131319020