Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global filters for MVC Gateway #3239

Closed
Andross96 opened this issue Jan 25, 2024 · 11 comments
Closed

Global filters for MVC Gateway #3239

Andross96 opened this issue Jan 25, 2024 · 11 comments
Labels

Comments

@Andross96
Copy link

Andross96 commented Jan 25, 2024

Question

Using the reactive (webflux) Spring Cloud Gateway, we had a GlobalFilter which allowed to filter on matching configured route only.
What is the equivalent in MVC ?

According to the documentation and another related issues (#3143), we have now to use Spring WebMvc.fn.
But we can not find a way using WebMvc.fn to simply filter on an already existing configured route only: we have to recreate an entire RouterFunction, translate predicates from application properties, etc.


For a better code comprehension, we are simply trying to translate our previous webflux GlobalFilter to make it work on MVC gateway:

@Component
public class CustomFilter implements GlobalFilter {
    // Adding a custom header for routed requests (existing in configuration, no matter the route and/or http method)
    @Override
    public Mono<Void> filter(final ServerWebExchange exchange, final GatewayFilterChain chain) {
        // We can even get the configuration of the route: final Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
        final ServerHttpRequest request = exchange.getRequest()
                .mutate()
                .header("custom", customHeaderValue) // adding our custom header
                .build();
        return chain.filter(exchange.mutate().request(request).build());
    }
}
@Andross96
Copy link
Author

Hello @NiiazKhan, thank you for your answer.
Unfortunately, this does not help because we still have to manually create RouterFunctions and translate everything from application properties (including predicates..) in order to create our filter. We are really looking for a reactive GlobalFilter like in MVC.

@NiiazKhan
Copy link

@Andross96 I am monitoring these issues:
#3250
#3268

@spencergibb
Copy link
Member

I'm not sure mvc gateway requires a separate implementation of filters besides what mvc.fn provides or just a servlet filter

@Andross96
Copy link
Author

We actually have no "proper" way to create filter on matched gateway routes configured inside application.yml, respecting the configured predicates.

Let's take a simple example:

spring:
  cloud:
    gateway:
      mvc:
        routes:
          - id: backend
            uri: http://localhost:8080
            predicates:
              - Path=/api/**
              # possibily multiple/variable predicates per route

Using the actual mvc.fn or servlet filter, we have to recreate the entire RouterFunction based on the configuration file, so we have to get the properties of the routes (to set the backend url), and the harsh part is to "translate" all predicates entered (like GatewayMvcPropertiesBeanDefinitionRegistrar#getRouterFunction) because we need the predicates to be validated first before executing our filter.

Are we missing something ?

@spencergibb
Copy link
Member

So you want filters that only run for gateway matched routes. What about #3177 default filters for mvc gateway?

@Andross96
Copy link
Author

Yes exactly, but there is a main difference which make the default filter still not as suitable as global filters unfortunately : with default filter, you have to write manually the filters in your application properties (sometimes multiple,complexes..), while global filters are executed by default for every matched routes, without worrying about additional application properties configuration.

It's a bit sad that migrating from reactive gateway to MVC isn't feasible while preserving identical behaviors. Both versions should ideally offer comparable features, ensuring consistency and compatibility across applications stack.

@spencergibb
Copy link
Member

It's a bit sad that migrating from reactive gateway to MVC isn't feasible while preserving identical behaviors.

This is never going to be possible to have identical behavior, similar yes, identical never.

So, to create a global filter you have to create a bean, why can't default filters be added with a bean?

@Andross96
Copy link
Author

That could do the trick ! But I do not see how to add default filter with a bean ?
The only documentation on default filter is related to writing properties, not beans ?

@spencergibb
Copy link
Member

spencergibb commented Feb 27, 2024

gateway mvc doesn't support default filters at all yet, so both config and bean configuration would be supported. That documentation if for the reactive gateway.

@spencergibb
Copy link
Member

Closing in favor of #3177

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants