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

SpringCloud gateway 3.1.1-GlobalFilter does not take effect

Open Layfolk-zcy opened this issue 2 years ago • 2 comments

@Component public class AuthGlobalFilter implements Ordered, GlobalFilter {

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
   // The code will not execute
    System.out.println("filter global");
    return chain.filter(exchange);
}

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

}

@RestController public class TestController {

@GetMapping("test")
public String test(){
    return "test 8888";
}

}

image

Layfolk-zcy avatar Aug 01 '22 08:08 Layfolk-zcy

you have to config it to a route.

    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(p -> p
                        .path("/get")
                        .filters(f -> f.addRequestHeader("Hello", "World"))
                        .uri("http://httpbin.org:80"))
                .route(p -> p
                        .path("/test")
                        .filters(f -> f.filter(new YourGatewayFilter()))
                        .uri("http://httpbin.org:80")).
                build();
    }

ssr66994053 avatar Aug 03 '22 12:08 ssr66994053

you have to config it to a route.

    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(p -> p
                        .path("/get")
                        .filters(f -> f.addRequestHeader("Hello", "World"))
                        .uri("http://httpbin.org:80"))
                .route(p -> p
                        .path("/test")
                        .filters(f -> f.filter(new YourGatewayFilter()))
                        .uri("http://httpbin.org:80")).
                build();
    }

but the route information is definited in yml.application. why i need to definit above the code.

Layfolk-zcy avatar Aug 10 '22 08:08 Layfolk-zcy

i am faced with the same issue. has this issue been solved?

miaomiao1992 avatar Oct 23 '22 12:10 miaomiao1992

i am faced with the same issue. has this issue been solved?

# config bootstrap.yml or application.yml
spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: false
    routes:
      - id: xxx

ssr66994053 avatar Oct 26 '22 05:10 ssr66994053

you have to config it to a route.

    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(p -> p
                        .path("/get")
                        .filters(f -> f.addRequestHeader("Hello", "World"))
                        .uri("http://httpbin.org:80"))
                .route(p -> p
                        .path("/test")
                        .filters(f -> f.filter(new YourGatewayFilter()))
                        .uri("http://httpbin.org:80")).
                build();
    }

but the route information is definited in yml.application. why i need to definit above the code.

config file yml is ok, but you need do config like below:

# config bootstrap.yml or application.yml
spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: false
    routes:
      - id: xxx

ssr66994053 avatar Oct 26 '22 05:10 ssr66994053