# Conflicts: # sop-gateway/pom.xml # sop-website/pom.xml # sop-website/sop-website-server/pom.xmleureka
commit
f33d69c139
@ -1,34 +1,34 @@ |
||||
* [首页](/?t=1595931646391) |
||||
* [首页](/?t=1606133657136) |
||||
* 开发文档 |
||||
* [快速体验](files/10010_快速体验.md?t=1595931646394) |
||||
* [项目接入到SOP](files/10011_项目接入到SOP.md?t=1595931646412) |
||||
* [新增接口](files/10020_新增接口.md?t=1595931646412) |
||||
* [开发流程](files/10021_开发流程.md?t=1595931646412) |
||||
* [业务参数校验](files/10030_业务参数校验.md?t=1595931646412) |
||||
* [错误处理](files/10040_错误处理.md?t=1595931646412) |
||||
* [编写文档](files/10041_编写文档.md?t=1595931646412) |
||||
* [接口交互详解](files/10050_接口交互详解.md?t=1595931646412) |
||||
* [使用签名校验工具](files/10080_使用签名校验工具.md?t=1595931646413) |
||||
* [ISV管理](files/10085_ISV管理.md?t=1595931646413) |
||||
* [自定义返回结果](files/10087_自定义返回结果.md?t=1595931646413) |
||||
* [自定义过滤器](files/10088_自定义过滤器.md?t=1595931646413) |
||||
* [自定义校验token](files/10089_自定义校验token.md?t=1595931646413) |
||||
* [网关拦截器](files/10090_网关拦截器.md?t=1595931646413) |
||||
* [路由授权](files/10090_路由授权.md?t=1595931646413) |
||||
* [接口限流](files/10092_接口限流.md?t=1595931646413) |
||||
* [路由监控](files/10093_路由监控.md?t=1595931646413) |
||||
* [SDK开发](files/10095_SDK开发.md?t=1595931646413) |
||||
* [应用授权](files/10097_应用授权.md?t=1595931646414) |
||||
* [提供restful接口](files/10100_提供restful接口.md?t=1595931646414) |
||||
* [文件上传](files/10104_文件上传.md?t=1595931646414) |
||||
* [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1595931646414) |
||||
* [预发布灰度发布](files/10110_预发布灰度发布.md?t=1595931646414) |
||||
* [动态修改请求参数](files/10111_动态修改请求参数.md?t=1595931646414) |
||||
* [使用eureka](files/10112_使用eureka.md?t=1595931646414) |
||||
* [快速体验](files/10010_快速体验.md?t=1606133657138) |
||||
* [项目接入到SOP](files/10011_项目接入到SOP.md?t=1606133657161) |
||||
* [新增接口](files/10020_新增接口.md?t=1606133657161) |
||||
* [开发流程](files/10021_开发流程.md?t=1606133657161) |
||||
* [业务参数校验](files/10030_业务参数校验.md?t=1606133657161) |
||||
* [错误处理](files/10040_错误处理.md?t=1606133657161) |
||||
* [编写文档](files/10041_编写文档.md?t=1606133657162) |
||||
* [接口交互详解](files/10050_接口交互详解.md?t=1606133657162) |
||||
* [使用签名校验工具](files/10080_使用签名校验工具.md?t=1606133657162) |
||||
* [ISV管理](files/10085_ISV管理.md?t=1606133657162) |
||||
* [自定义返回结果](files/10087_自定义返回结果.md?t=1606133657162) |
||||
* [自定义过滤器](files/10088_自定义过滤器.md?t=1606133657162) |
||||
* [自定义校验token](files/10089_自定义校验token.md?t=1606133657162) |
||||
* [网关拦截器](files/10090_网关拦截器.md?t=1606133657163) |
||||
* [路由授权](files/10091_路由授权.md?t=1606133657163) |
||||
* [接口限流](files/10092_接口限流.md?t=1606133657163) |
||||
* [路由监控](files/10093_路由监控.md?t=1606133657163) |
||||
* [SDK开发](files/10095_SDK开发.md?t=1606133657163) |
||||
* [应用授权](files/10097_应用授权.md?t=1606133657163) |
||||
* [提供restful接口](files/10100_提供restful接口.md?t=1606133657163) |
||||
* [文件上传](files/10104_文件上传.md?t=1606133657163) |
||||
* [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1606133657164) |
||||
* [预发布灰度发布](files/10110_预发布灰度发布.md?t=1606133657164) |
||||
* [动态修改请求参数](files/10111_动态修改请求参数.md?t=1606133657164) |
||||
* [使用eureka](files/10112_使用eureka.md?t=1606133657164) |
||||
* 原理分析 |
||||
* [网关性能测试](files/90001_网关性能测试.md?t=1595931646414) |
||||
* [原理分析之如何存储路由](files/90011_原理分析之如何存储路由.md?t=1595931646415) |
||||
* [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1595931646415) |
||||
* [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1595931646415) |
||||
* [原理分析之预发布灰度发布](files/90014_原理分析之预发布灰度发布.md?t=1595931646415) |
||||
* [常见问题](files/90100_常见问题.md?t=1595931646415) |
||||
* [网关性能测试](files/90001_网关性能测试.md?t=1606133657164) |
||||
* [原理分析之如何存储路由](files/90011_原理分析之如何存储路由.md?t=1606133657164) |
||||
* [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1606133657164) |
||||
* [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1606133657164) |
||||
* [原理分析之预发布灰度发布](files/90014_原理分析之预发布灰度发布.md?t=1606133657165) |
||||
* [常见问题](files/90100_常见问题.md?t=1606133657165) |
||||
|
@ -1,103 +1,39 @@ |
||||
# 自定义过滤器 |
||||
|
||||
## zuul过滤器 |
||||
|
||||
zuul过滤器列表如下: |
||||
|
||||
| 类型 | 顺序 | 过滤器 | 功能 | |
||||
| ----- | ---- | ----------------------- | ---------------------------- | |
||||
| pre | -1000 | PreValidateFilter (SOP自带) | 校验签名 | |
||||
| pre | -998 | PreLimitFilter (SOP自带) | 限流拦截器 | |
||||
| pre | -3 | ServletDetectionFilter | 标记处理 Servlet 的类型 | |
||||
| pre | -2 | Servlet30WrapperFilter | 包装 HttpServletRequest 请求 | |
||||
| pre | -1 | FormBodyWrapperFilter | 包装请求体 Servlet30WrapperFilter | |
||||
| 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 ~ -501留给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同理 |
||||
演示在网关追加一个header |
||||
|
||||
```java |
||||
public class XXXFilter implements GlobalFilter, Ordered { |
||||
public class CustomFilter 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); |
||||
// 演示在网关追加header |
||||
ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange); |
||||
String token = apiParam.fetchAccessToken(); |
||||
ServerWebExchange serverWebExchange = ServerWebExchangeUtil.addHeaders(exchange, httpHeaders -> { |
||||
httpHeaders.add("token", token); |
||||
}); |
||||
return chain.filter(serverWebExchange); |
||||
} |
||||
|
||||
@Override |
||||
public int getOrder() { |
||||
return -500; |
||||
// 自定义过滤器可以从0开始 |
||||
return 0; |
||||
} |
||||
} |
||||
``` |
||||
|
||||
使用过滤器: |
||||
使用过滤器,在sop-gateway中找到MyConfig,添加: |
||||
|
||||
```java |
||||
@Configuration |
||||
public class GatewayConfig extends AlipayGatewayConfiguration { |
||||
public class MyConfig { |
||||
|
||||
... |
||||
@Bean |
||||
XXXFilter xxxFilter() { |
||||
return new XXXFilter(); |
||||
CustomFilter customFilter() { |
||||
return new CustomFilter(); |
||||
} |
||||
... |
||||
} |
||||
|
Loading…
Reference in new issue