spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

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

Open gbaso opened this issue 1 year ago • 3 comments

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.

gbaso avatar Feb 02 '24 13:02 gbaso

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

spencergibb avatar Mar 11 '24 16:03 spencergibb

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

gbaso avatar Mar 11 '24 22:03 gbaso

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.

jensmatw avatar Jul 29 '24 10:07 jensmatw

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

ciscoo avatar Nov 01 '24 17:11 ciscoo

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

spencergibb avatar Nov 01 '24 17:11 spencergibb

waiting for this to switch from webflux to MVC

any updates?

dominik-kovacs avatar Jan 31 '25 15:01 dominik-kovacs

No, but it is something to do for this upcoming release in May

spencergibb avatar Jan 31 '25 16:01 spencergibb

Please follow #3763

spencergibb avatar Apr 14 '25 21:04 spencergibb