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

Allow registering custom filters and predicates in programmatic way (not spring.factories) #3250

Open
gbaso opened this issue Feb 2, 2024 · 5 comments

Comments

@gbaso
Copy link

gbaso commented Feb 2, 2024

According to the documentation, if you want to use a custom filter for your route you have to publish a RouterFunction bean with the specified filter, with no alternatives if you want to configure the routing via properties. By contrast, in the reactive version of gateway you can publish a GatewayFilterFactory and reference the filter in your properties. It would be useful to be able to do the same with HandlerFilterFunctions.

At the moment the only way to do this is to create a FilterSupplier for your filters and reference it in a spring.factories file. Registering the FilterSupplier as bean does not work, since GatewayMvcPropertiesBeanDefinitionRegistrar does not perform any bean resolution, instead it delegates to the FilterDiscoverer which in turns uses SpringFactoriesLoader.loadFactories. While this works fine, it's cumbersome and it would be greatly simplified if you could directly publish the filters.

@spencergibb
Copy link
Member

Registering FilterSupplier or PredicateSupplier is currently only supported via spring.factories. A BeanDefinitionRegistrar happens very early in the spring IoC lifecycle and I don't know how to get all the information as a bean. What about classpath scanning like for openfeign or spring data?

https://docs.spring.io/spring-cloud-openfeign/reference/spring-cloud-openfeign.html#netflix-feign-starter

@gbaso
Copy link
Author

gbaso commented Mar 11, 2024

Requiring annotations or interface extensions is also fine by me. I just want to avoid writing a spring.factories, which is error prone due to the lack of type safety

@spencergibb spencergibb changed the title Allow registering custom filters as beans in gateway mvc Allow registering custom filters and predicates in programmatic way (not spring.factories) Mar 11, 2024
@spencergibb spencergibb moved this to Todo in 2024.0.0-M1 Mar 25, 2024
@jensmatw
Copy link
Contributor

Is it somehow possible to add Before/After filters via spring.factories? It seems the "out-of-the-box" filters from SCG-mvc are using a wrapper class which is registered as full filters, with just one-liners that call the Before/After filter. Could do that for mine as well like this, but a more convient way as we had in the reactive gateway would be more comfortable.

@ciscoo
Copy link

ciscoo commented Nov 1, 2024

Could RouterFunctionHolderFactory implement FactoryBean<Object>? This can then implement BeanFactoryAware to get access to other beans such as those that implement FilterSupplier

@spencergibb
Copy link
Member

@ciscoo maybe. I'd have to do some design before I start looking at this feature.

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

No branches or pull requests

5 participants