From 1c0a62eb771106e2c938a88c154d93e00f3e9c6e Mon Sep 17 00:00:00 2001 From: tanghc Date: Mon, 13 May 2019 18:18:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=87=E6=BB=A4=E5=99=A8=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/docs/_sidebar.md | 47 ++++---- doc/docs/files/10102_自定义过滤器.md | 105 ++++++++++++++++++ .../zuul/filter/BaseZuulFilter.java | 1 - .../zuul/filter/PreRoutePermissionFilter.java | 2 +- 4 files changed, 130 insertions(+), 25 deletions(-) create mode 100644 doc/docs/files/10102_自定义过滤器.md diff --git a/doc/docs/_sidebar.md b/doc/docs/_sidebar.md index eef283be..acbcc13b 100644 --- a/doc/docs/_sidebar.md +++ b/doc/docs/_sidebar.md @@ -1,25 +1,26 @@ -* [首页](/?t=1557382242590) +* [首页](/?t=1557742456964) * 开发文档 - * [快速体验](files/10010_快速体验.md?t=1557382242593) - * [项目接入到SOP](files/10011_项目接入到SOP.md?t=1557382242610) - * [新增接口](files/10020_新增接口.md?t=1557382242610) - * [业务参数校验](files/10030_业务参数校验.md?t=1557382242610) - * [错误处理](files/10040_错误处理.md?t=1557382242610) - * [编写文档](files/10041_编写文档.md?t=1557382242610) - * [接口交互详解](files/10050_接口交互详解.md?t=1557382242610) - * [easyopen支持](files/10070_easyopen支持.md?t=1557382242610) - * [使用签名校验工具](files/10080_使用签名校验工具.md?t=1557382242611) - * [ISV管理](files/10085_ISV管理.md?t=1557382242611) - * [路由授权](files/10090_路由授权.md?t=1557382242611) - * [接口限流](files/10092_接口限流.md?t=1557382242611) - * [SDK开发](files/10095_SDK开发.md?t=1557382242611) - * [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1557382242611) - * [应用授权](files/10097_应用授权.md?t=1557382242611) - * [更改数据节点名称](files/10099_更改数据节点名称.md?t=1557382242612) - * [对接前端](files/10100_对接前端.md?t=1557382242612) + * [快速体验](files/10010_快速体验.md?t=1557742456965) + * [项目接入到SOP](files/10011_项目接入到SOP.md?t=1557742456983) + * [新增接口](files/10020_新增接口.md?t=1557742456983) + * [业务参数校验](files/10030_业务参数校验.md?t=1557742456983) + * [错误处理](files/10040_错误处理.md?t=1557742456983) + * [编写文档](files/10041_编写文档.md?t=1557742456984) + * [接口交互详解](files/10050_接口交互详解.md?t=1557742456984) + * [easyopen支持](files/10070_easyopen支持.md?t=1557742456984) + * [使用签名校验工具](files/10080_使用签名校验工具.md?t=1557742456984) + * [ISV管理](files/10085_ISV管理.md?t=1557742456984) + * [路由授权](files/10090_路由授权.md?t=1557742456984) + * [接口限流](files/10092_接口限流.md?t=1557742456984) + * [SDK开发](files/10095_SDK开发.md?t=1557742456984) + * [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1557742456984) + * [应用授权](files/10097_应用授权.md?t=1557742456984) + * [更改数据节点名称](files/10099_更改数据节点名称.md?t=1557742456984) + * [对接前端](files/10100_对接前端.md?t=1557742456985) + * [自定义过滤器](files/10102_自定义过滤器.md?t=1557742456985) * 原理分析 - * [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1557382242612) - * [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1557382242612) - * [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1557382242612) - * [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1557382242612) - * [常见问题](files/90100_常见问题.md?t=1557382242612) + * [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1557742456985) + * [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1557742456985) + * [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1557742456985) + * [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1557742456985) + * [常见问题](files/90100_常见问题.md?t=1557742456985) diff --git a/doc/docs/files/10102_自定义过滤器.md b/doc/docs/files/10102_自定义过滤器.md new file mode 100644 index 00000000..fcd6d02b --- /dev/null +++ b/doc/docs/files/10102_自定义过滤器.md @@ -0,0 +1,105 @@ +# 自定义过滤器 + +## zuul过滤器 + +zuul过滤器列表如下: + +| 类型 | 顺序 | 过滤器 | 功能 | +| ----- | ---- | ----------------------- | ---------------------------- | +| pre | -1000 | PreValidateFilter (SOP自带) | 校验签名 | +| pre | -999 | PreRoutePermissionFilter (SOP自带) | 路由权限校验,有些接口需要配置权限才能访问 | +| pre | -998 | PreLimitFilter (SOP自带) | 限流拦截器 | +| pre | -3 | ServletDetectionFilter | 标记处理 Servlet 的类型 | +| pre | -2 | Servlet30WrapperFilter | 包装 HttpServletRequest 请求 | +| pre | -1 | FormBodyWrapperFilter | 包装请求体 | +| 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自带的过滤器。 + +创建自定义过滤器可以从`-500`开始(-1000 ~ -499留给SOP)。下面是一个自定义过虑器的例子: + +```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 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(); + } + ... +} +``` \ No newline at end of file diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/BaseZuulFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/BaseZuulFilter.java index 0abc09a4..ca0ac2c8 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/BaseZuulFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/BaseZuulFilter.java @@ -1,7 +1,6 @@ package com.gitee.sop.gatewaycommon.zuul.filter; import com.alibaba.fastjson.JSON; -import com.gitee.sop.gatewaycommon.result.ApiResult; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreRoutePermissionFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreRoutePermissionFilter.java index 0c37fb8a..37cda8e7 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreRoutePermissionFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreRoutePermissionFilter.java @@ -25,7 +25,7 @@ public class PreRoutePermissionFilter extends BaseZuulFilter { @Override protected int getFilterOrder() { // 放在签名验证后面 - return PRE_VALIDATE_FILTER_ORDER + 1; + return PRE_ROUTE_PERMISSION_FILTER_ORDER; } @Override