You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SOP/doc/docs/files/10088_自定义过滤器.md

40 lines
944 B

6 years ago
# 自定义过滤器
4 years ago
演示在网关追加一个header
6 years ago
```java
4 years ago
public class CustomFilter implements GlobalFilter, Ordered {
6 years ago
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
4 years ago
// 演示在网关追加header
ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange);
String token = apiParam.fetchAccessToken();
ServerWebExchange serverWebExchange = ServerWebExchangeUtil.addHeaders(exchange, httpHeaders -> {
httpHeaders.add("token", token);
});
return chain.filter(serverWebExchange);
6 years ago
}
@Override
public int getOrder() {
4 years ago
// 自定义过滤器可以从0开始
return 0;
6 years ago
}
}
```
4 years ago
使用过滤器,在sop-gateway中找到MyConfig,添加:
6 years ago
```java
@Configuration
4 years ago
public class MyConfig {
6 years ago
...
@Bean
4 years ago
CustomFilter customFilter() {
return new CustomFilter();
6 years ago
}
...
}
```