Filters for all routes in Spring Cloud Gateway MVC

40 views Asked by At

I'm using Spring Cloud Gateway MVC and need to know how to apply a filter to all routes. I've tried configuring spring.cloud.gateway.default-filters in application.yml, however, this configuration is for Spring Cloud Gateway (reactive) and doesn't work in MVC.

I would like a way to achieve this either in YAML or even through a Java class.

Spring Cloud Gateway MVC dependency:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
</dependency>

This way works, but I would have to replicate this for all route classes, and there are many of them.

@Bean
public RouterFunction<ServerResponse> getRoutesCadastrosAlcada() {
    return route()
            .POST("someUrl", http(URL_GPOC_CADASTROS))
            .POST("someUrl", http(URL_GPOC_CADASTROS))
            .POST("someUrl", http(URL_GPOC_CADASTROS))
            .POST("someUrl", http(URL_GPOC_CADASTROS))
            .POST("someUrl", http(URL_GPOC_CADASTROS))
            .POST("someUrl", http(URL_GPOC_CADASTROS))
            .after(removeResponseHeader("Access-Control-Allow-Origin"))
            .build();
}
0

There are 0 answers