spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.52k stars 3.32k forks source link

SpringCloud gateway 3.1.1-GlobalFilter does not take effect #2689

Closed Layfolk-zcy closed 2 years ago

Layfolk-zcy commented 2 years ago

@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

ssr66994053 commented 2 years ago

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();
    }
Layfolk-zcy commented 2 years ago

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.

miaomiao1992 commented 2 years ago

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

ssr66994053 commented 2 years ago

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 commented 2 years ago

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