如何在 application.yml Spring 云网关中指定自定义过滤器
How to Specify custom filter in application.yml Spring Cloud Gateway
我有一个自定义网关过滤器 MYGatewayFilter.java 文件,现在我想将此网关过滤器与我在 application.yml
中编写的路由一起使用
spring:
cloud:
gateway:
routes:
- id: login2_route
uri: http://127.0.0.1:8083/login
predicates:
- Path: /login/
filters:
如何为上述路由定义过滤器
自定义过滤器MyGatewayFilter.java
public class MyGatewayFilter implements GatewayFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request;
if(request.getHeaders().get("x-mydata")!=null){
request= exchange.getRequest().mutate().header("my-new-header",request.getHeaders().get("x-mydata").get(0)).build();
}
return chain.filter(exchange.mutate().request(request).build());
}
}
您应该实施 GatewayFilterFactory 而不是实施 GatewayFilter
并使其成为组件:
@Component
public class MyGatewayFilter implements GatewayFilterFactory {
然后你就可以通过你路由中的bean名来引用了。
filters:
- MyGatewayFilter
目前这方面的文档不是很好。我只能通过查看 github
上 spring-cloud-gateway 的源代码来解决这个问题
您需要实施GatewayFilterFactory
@Component
public class DemoGatewayFilter implements GatewayFilterFactory<DemoGatewayFilter.Config> {
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
System.out.println("gateway filter name "+config.getName());
return chain.filter(exchange);
};
}
@Override
public Config newConfig() {
return new Config("DemoGatewayFilter");
}
public static class Config {
public Config(String name){
this.name = name;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
并在 application.yml 文件中
spring:
application:
cloud:
gateway:
routes:
- id: MayApplication
uri: http://myapplication:8080
predicates:
- Path=/apipath/to/filter/**
filters:
- DemoGatewayFilter
我认为 Spring Cloud Gateway 2.2.1 中有一些变化。
我提到了 SaveSessionGatewayFilterFactory.java。
@Component
public class DemoGatewayFilter implements AbstractGatewayFilterFactory {
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> {
return chain.filter(exchange);
};
/*
it works too
return new GatewayFilter() {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return chain.filter(exchange);
}
}
*/
}
}
并在 application.yml 文件中
spring:
application:
cloud:
gateway:
routes:
- id: MayApplication
uri: http://myapplication:8080
predicates:
- Path=/api/path/to/filter/**
filters:
- DemoGatewayFilter
我有一个自定义网关过滤器 MYGatewayFilter.java 文件,现在我想将此网关过滤器与我在 application.yml
中编写的路由一起使用 spring:
cloud:
gateway:
routes:
- id: login2_route
uri: http://127.0.0.1:8083/login
predicates:
- Path: /login/
filters:
如何为上述路由定义过滤器
自定义过滤器MyGatewayFilter.java
public class MyGatewayFilter implements GatewayFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request;
if(request.getHeaders().get("x-mydata")!=null){
request= exchange.getRequest().mutate().header("my-new-header",request.getHeaders().get("x-mydata").get(0)).build();
}
return chain.filter(exchange.mutate().request(request).build());
}
}
您应该实施 GatewayFilterFactory 而不是实施 GatewayFilter
并使其成为组件:
@Component
public class MyGatewayFilter implements GatewayFilterFactory {
然后你就可以通过你路由中的bean名来引用了。
filters:
- MyGatewayFilter
目前这方面的文档不是很好。我只能通过查看 github
上 spring-cloud-gateway 的源代码来解决这个问题您需要实施GatewayFilterFactory
@Component
public class DemoGatewayFilter implements GatewayFilterFactory<DemoGatewayFilter.Config> {
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
System.out.println("gateway filter name "+config.getName());
return chain.filter(exchange);
};
}
@Override
public Config newConfig() {
return new Config("DemoGatewayFilter");
}
public static class Config {
public Config(String name){
this.name = name;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
并在 application.yml 文件中
spring:
application:
cloud:
gateway:
routes:
- id: MayApplication
uri: http://myapplication:8080
predicates:
- Path=/apipath/to/filter/**
filters:
- DemoGatewayFilter
我认为 Spring Cloud Gateway 2.2.1 中有一些变化。
我提到了 SaveSessionGatewayFilterFactory.java。
@Component
public class DemoGatewayFilter implements AbstractGatewayFilterFactory {
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> {
return chain.filter(exchange);
};
/*
it works too
return new GatewayFilter() {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return chain.filter(exchange);
}
}
*/
}
}
并在 application.yml 文件中
spring:
application:
cloud:
gateway:
routes:
- id: MayApplication
uri: http://myapplication:8080
predicates:
- Path=/api/path/to/filter/**
filters:
- DemoGatewayFilter