spring-cloud-gateway
spring-cloud-gateway copied to clipboard
SpringCloud gateway 3.1.1-GlobalFilter does not take effect
@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";
}
}
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();
}
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.
i am faced with the same issue. has this issue been solved?
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
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