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

104 lines
2.8 KiB

5 years ago
# 自定义过滤器
## zuul过滤器
zuul过滤器列表如下:
| 类型 | 顺序 | 过滤器 | 功能 |
| ----- | ---- | ----------------------- | ---------------------------- |
| pre | -1000 | PreValidateFilter (SOP自带) | 校验签名 |
| pre | -998 | PreLimitFilter (SOP自带) | 限流拦截器 |
| pre | -3 | ServletDetectionFilter | 标记处理 Servlet 的类型 |
| pre | -2 | Servlet30WrapperFilter | 包装 HttpServletRequest 请求 |
| pre | -1 | FormBodyWrapperFilter | 包装请求体 Servlet30WrapperFilter |
5 years ago
| pre | 1 | DebugFilter | 标记调试标志 |
| pre | 5 | PreDecorationFilter | 决定路由转发过滤器 |
| route | 10 | RibbonRoutingFilter | serviceId 请求转发 |
| route | 100 | SimpleHostRoutingFilter | url 请求转发 |
| route | 500 | SendForwardFilter | forward 请求转发 |
| post | 0 | SendErrorFilter | 处理有错误的请求响应 |
| post | 1000 | SendResponseFilter | 处理正常的请求响应 |
顺序值小的优先执行,`-3`之前是sop自带的过滤器,`-3`开始是zuul自带的过滤器。
5 years ago
创建自定义过滤器可以从`-500`开始(-1000 ~ -501留给SOP)。下面是一个自定义过虑器的例子:
5 years ago
```java
public class PreXXXFilter extends BaseZuulFilter {
@Override
protected FilterType getFilterType() {
return FilterType.PRE;
}
@Override
protected int getFilterOrder() {
return -500;
}
@Override
protected Object doRun(RequestContext requestContext) throws ZuulException {
HttpServletRequest request = requestContext.getRequest();
ApiParam apiParam = ZuulContext.getApiParam();
String appKey = apiParam.fetchAppKey();
// ...业务处理
// 固定返回null
return null;
}
}
```
过滤器编写完毕后,在Config中使用:
```java
@Configuration
public class ZuulConfig extends AlipayZuulConfiguration {
...
@Bean
PreXXXFilter preXXXFilter() {
return new PreXXXFilter();
}
...
}
```
## spring cloud gateway
跟zuul同理
```java
public class XXXFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ApiParam apiParam = (ApiParam)exchange.getAttribute(SopConstants.CACHE_API_PARAM);
String appKey = apiParam.fetchAppKey();
// ...业务处理
...
return chain.filter(exchange);
}
@Override
public int getOrder() {
return -500;
}
}
```
使用过滤器:
```java
@Configuration
public class GatewayConfig extends AlipayGatewayConfiguration {
...
@Bean
XXXFilter xxxFilter() {
return new XXXFilter();
}
...
}
```