mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
gateway动态修改参数
This commit is contained in:
@@ -1,34 +1,34 @@
|
|||||||
* [首页](/?t=1565767132305)
|
* [首页](/?t=1565916799573)
|
||||||
* 开发文档
|
* 开发文档
|
||||||
* [快速体验](files/10010_快速体验.md?t=1565767132307)
|
* [快速体验](files/10010_快速体验.md?t=1565916799577)
|
||||||
* [项目接入到SOP](files/10011_项目接入到SOP.md?t=1565767132325)
|
* [项目接入到SOP](files/10011_项目接入到SOP.md?t=1565916799596)
|
||||||
* [新增接口](files/10020_新增接口.md?t=1565767132325)
|
* [新增接口](files/10020_新增接口.md?t=1565916799596)
|
||||||
* [业务参数校验](files/10030_业务参数校验.md?t=1565767132325)
|
* [业务参数校验](files/10030_业务参数校验.md?t=1565916799596)
|
||||||
* [错误处理](files/10040_错误处理.md?t=1565767132325)
|
* [错误处理](files/10040_错误处理.md?t=1565916799596)
|
||||||
* [编写文档](files/10041_编写文档.md?t=1565767132326)
|
* [编写文档](files/10041_编写文档.md?t=1565916799596)
|
||||||
* [接口交互详解](files/10050_接口交互详解.md?t=1565767132326)
|
* [接口交互详解](files/10050_接口交互详解.md?t=1565916799597)
|
||||||
* [easyopen支持](files/10070_easyopen支持.md?t=1565767132326)
|
* [easyopen支持](files/10070_easyopen支持.md?t=1565916799597)
|
||||||
* [使用签名校验工具](files/10080_使用签名校验工具.md?t=1565767132326)
|
* [使用签名校验工具](files/10080_使用签名校验工具.md?t=1565916799597)
|
||||||
* [ISV管理](files/10085_ISV管理.md?t=1565767132326)
|
* [ISV管理](files/10085_ISV管理.md?t=1565916799597)
|
||||||
* [自定义路由](files/10086_自定义路由.md?t=1565767132326)
|
* [自定义路由](files/10086_自定义路由.md?t=1565916799597)
|
||||||
* [自定义返回结果](files/10087_自定义返回结果.md?t=1565767132326)
|
* [自定义返回结果](files/10087_自定义返回结果.md?t=1565916799597)
|
||||||
* [自定义过滤器](files/10088_自定义过滤器.md?t=1565767132326)
|
* [自定义过滤器](files/10088_自定义过滤器.md?t=1565916799597)
|
||||||
* [路由授权](files/10090_路由授权.md?t=1565767132326)
|
* [路由授权](files/10090_路由授权.md?t=1565916799598)
|
||||||
* [接口限流](files/10092_接口限流.md?t=1565767132326)
|
* [接口限流](files/10092_接口限流.md?t=1565916799598)
|
||||||
* [监控日志](files/10093_监控日志.md?t=1565767132327)
|
* [监控日志](files/10093_监控日志.md?t=1565916799598)
|
||||||
* [SDK开发](files/10095_SDK开发.md?t=1565767132327)
|
* [SDK开发](files/10095_SDK开发.md?t=1565916799598)
|
||||||
* [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1565767132327)
|
* [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1565916799598)
|
||||||
* [应用授权](files/10097_应用授权.md?t=1565767132327)
|
* [应用授权](files/10097_应用授权.md?t=1565916799598)
|
||||||
* [传统web开发](files/10100_传统web开发.md?t=1565767132327)
|
* [传统web开发](files/10100_传统web开发.md?t=1565916799598)
|
||||||
* [文件上传](files/10104_文件上传.md?t=1565767132327)
|
* [文件上传](files/10104_文件上传.md?t=1565916799598)
|
||||||
* [nacos注册中心](files/10106_nacos注册中心.md?t=1565767132327)
|
* [nacos注册中心](files/10106_nacos注册中心.md?t=1565916799599)
|
||||||
* [扩展其它注册中心](files/10107_扩展其它注册中心.md?t=1565767132327)
|
* [扩展其它注册中心](files/10107_扩展其它注册中心.md?t=1565916799599)
|
||||||
* [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1565767132327)
|
* [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1565916799599)
|
||||||
* [预发布灰度发布](files/10110_预发布灰度发布.md?t=1565767132327)
|
* [预发布灰度发布](files/10110_预发布灰度发布.md?t=1565916799599)
|
||||||
* [动态修改请求参数](files/10111_动态修改请求参数.md?t=1565767132327)
|
* [动态修改请求参数](files/10111_动态修改请求参数.md?t=1565916799599)
|
||||||
* 原理分析
|
* 原理分析
|
||||||
* [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1565767132328)
|
* [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1565916799599)
|
||||||
* [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1565767132328)
|
* [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1565916799599)
|
||||||
* [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1565767132328)
|
* [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1565916799599)
|
||||||
* [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1565767132328)
|
* [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1565916799600)
|
||||||
* [常见问题](files/90100_常见问题.md?t=1565767132328)
|
* [常见问题](files/90100_常见问题.md?t=1565916799600)
|
||||||
|
@@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ApiConfig.getInstance().setZuulParameterFormatter(requestParams -> {
|
ApiConfig.getInstance().setParameterFormatter(requestParams -> {
|
||||||
// 获取biz_content
|
// 获取biz_content
|
||||||
JSONObject jsonObject = requestParams.getJSONObject(ParamNames.BIZ_CONTENT_NAME);
|
JSONObject jsonObject = requestParams.getJSONObject(ParamNames.BIZ_CONTENT_NAME);
|
||||||
// 修改biz_content中的值
|
// 修改biz_content中的值
|
||||||
|
@@ -18,6 +18,7 @@ import com.gitee.sop.gatewaycommon.manager.LimitConfigManager;
|
|||||||
import com.gitee.sop.gatewaycommon.manager.RouteConfigManager;
|
import com.gitee.sop.gatewaycommon.manager.RouteConfigManager;
|
||||||
import com.gitee.sop.gatewaycommon.manager.ServiceErrorManager;
|
import com.gitee.sop.gatewaycommon.manager.ServiceErrorManager;
|
||||||
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
|
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
|
||||||
|
import com.gitee.sop.gatewaycommon.param.ParameterFormatter;
|
||||||
import com.gitee.sop.gatewaycommon.result.DataNameBuilder;
|
import com.gitee.sop.gatewaycommon.result.DataNameBuilder;
|
||||||
import com.gitee.sop.gatewaycommon.result.DefaultDataNameBuilder;
|
import com.gitee.sop.gatewaycommon.result.DefaultDataNameBuilder;
|
||||||
import com.gitee.sop.gatewaycommon.result.ResultAppender;
|
import com.gitee.sop.gatewaycommon.result.ResultAppender;
|
||||||
@@ -34,7 +35,6 @@ import com.gitee.sop.gatewaycommon.validate.Signer;
|
|||||||
import com.gitee.sop.gatewaycommon.validate.Validator;
|
import com.gitee.sop.gatewaycommon.validate.Validator;
|
||||||
import com.gitee.sop.gatewaycommon.zuul.configuration.ZuulErrorController;
|
import com.gitee.sop.gatewaycommon.zuul.configuration.ZuulErrorController;
|
||||||
import com.gitee.sop.gatewaycommon.zuul.param.ZuulParamBuilder;
|
import com.gitee.sop.gatewaycommon.zuul.param.ZuulParamBuilder;
|
||||||
import com.gitee.sop.gatewaycommon.zuul.param.ZuulParameterFormatter;
|
|
||||||
import com.gitee.sop.gatewaycommon.zuul.result.ZuulResultExecutor;
|
import com.gitee.sop.gatewaycommon.zuul.result.ZuulResultExecutor;
|
||||||
import com.netflix.zuul.context.RequestContext;
|
import com.netflix.zuul.context.RequestContext;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@@ -150,7 +150,7 @@ public class ApiConfig {
|
|||||||
*/
|
*/
|
||||||
private ZuulErrorController zuulErrorController = new ZuulErrorController();
|
private ZuulErrorController zuulErrorController = new ZuulErrorController();
|
||||||
|
|
||||||
private ZuulParameterFormatter zuulParameterFormatter;
|
private ParameterFormatter parameterFormatter;
|
||||||
|
|
||||||
// -------- fields ---------
|
// -------- fields ---------
|
||||||
|
|
||||||
|
@@ -125,5 +125,17 @@ public class ServerWebExchangeUtil {
|
|||||||
return new FileUploadHttpServletRequest(exchange.getRequest(), data);
|
return new FileUploadHttpServletRequest(exchange.getRequest(), data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改请求参数
|
||||||
|
* @param exchange ServerWebExchange
|
||||||
|
* @param apiParam 请求参数
|
||||||
|
* @param consumer 执行参数更改
|
||||||
|
* @param <T> 参数类型
|
||||||
|
* @return 返回新的ServerWebExchange
|
||||||
|
*/
|
||||||
|
public static <T extends Map<String, Object>> ServerWebExchange format(ServerWebExchange exchange, T apiParam, Consumer<T> consumer) {
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -3,6 +3,7 @@ package com.gitee.sop.gatewaycommon.gateway.configuration;
|
|||||||
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
||||||
import com.gitee.sop.gatewaycommon.gateway.filter.GatewayModifyResponseGatewayFilter;
|
import com.gitee.sop.gatewaycommon.gateway.filter.GatewayModifyResponseGatewayFilter;
|
||||||
import com.gitee.sop.gatewaycommon.gateway.filter.LoadBalancerClientExtFilter;
|
import com.gitee.sop.gatewaycommon.gateway.filter.LoadBalancerClientExtFilter;
|
||||||
|
import com.gitee.sop.gatewaycommon.gateway.filter.ParameterFormatterFilter;
|
||||||
import com.gitee.sop.gatewaycommon.gateway.filter.ValidateFilter;
|
import com.gitee.sop.gatewaycommon.gateway.filter.ValidateFilter;
|
||||||
import com.gitee.sop.gatewaycommon.gateway.handler.GatewayExceptionHandler;
|
import com.gitee.sop.gatewaycommon.gateway.handler.GatewayExceptionHandler;
|
||||||
import com.gitee.sop.gatewaycommon.gateway.route.GatewayRouteRepository;
|
import com.gitee.sop.gatewaycommon.gateway.route.GatewayRouteRepository;
|
||||||
@@ -106,4 +107,9 @@ public class BaseGatewayConfiguration extends AbstractConfiguration {
|
|||||||
return gatewayRouteRepository;
|
return gatewayRouteRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
ParameterFormatterFilter parameterFormatterFilter() {
|
||||||
|
return new ParameterFormatterFilter();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -3,7 +3,7 @@ package com.gitee.sop.gatewaycommon.gateway.filter;
|
|||||||
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
||||||
import com.gitee.sop.gatewaycommon.bean.ConfigLimitDto;
|
import com.gitee.sop.gatewaycommon.bean.ConfigLimitDto;
|
||||||
import com.gitee.sop.gatewaycommon.exception.ApiException;
|
import com.gitee.sop.gatewaycommon.exception.ApiException;
|
||||||
import com.gitee.sop.gatewaycommon.gateway.GatewayContext;
|
import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil;
|
||||||
import com.gitee.sop.gatewaycommon.limit.LimitManager;
|
import com.gitee.sop.gatewaycommon.limit.LimitManager;
|
||||||
import com.gitee.sop.gatewaycommon.limit.LimitType;
|
import com.gitee.sop.gatewaycommon.limit.LimitType;
|
||||||
import com.gitee.sop.gatewaycommon.manager.LimitConfigManager;
|
import com.gitee.sop.gatewaycommon.manager.LimitConfigManager;
|
||||||
@@ -34,7 +34,7 @@ public class LimitFilter implements GlobalFilter, Ordered {
|
|||||||
if (!apiConfig.isOpenLimit()) {
|
if (!apiConfig.isOpenLimit()) {
|
||||||
return chain.filter(exchange);
|
return chain.filter(exchange);
|
||||||
}
|
}
|
||||||
ApiParam apiParam = GatewayContext.getApiParam(exchange);
|
ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange);
|
||||||
ConfigLimitDto configLimitDto = this.findConfigLimitDto(apiConfig, apiParam, exchange);
|
ConfigLimitDto configLimitDto = this.findConfigLimitDto(apiConfig, apiParam, exchange);
|
||||||
if (configLimitDto == null) {
|
if (configLimitDto == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@@ -0,0 +1,41 @@
|
|||||||
|
package com.gitee.sop.gatewaycommon.gateway.filter;
|
||||||
|
|
||||||
|
import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil;
|
||||||
|
import com.gitee.sop.gatewaycommon.param.ApiParam;
|
||||||
|
import com.gitee.sop.gatewaycommon.param.ParamNames;
|
||||||
|
import com.gitee.sop.gatewaycommon.param.ParameterFormatter;
|
||||||
|
import com.gitee.sop.gatewaycommon.zuul.ZuulContext;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author tanghc
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class ParameterFormatterFilter implements GlobalFilter, Ordered {
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private ParameterFormatter sopParameterFormatter;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
ApiParam apiParam = ZuulContext.getApiParam();
|
||||||
|
// 校验成功后进行参数转换
|
||||||
|
if (sopParameterFormatter != null) {
|
||||||
|
ServerWebExchange formatExchange = ServerWebExchangeUtil.format(exchange, apiParam, sopParameterFormatter::format);
|
||||||
|
ServerWebExchange serverWebExchange = ServerWebExchangeUtil.addHeaders(formatExchange, httpHeaders -> httpHeaders.add(ParamNames.HEADER_VERSION_NAME, apiParam.fetchVersion()));
|
||||||
|
return chain.filter(serverWebExchange);
|
||||||
|
}
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
@@ -1,7 +1,7 @@
|
|||||||
package com.gitee.sop.gatewaycommon.gateway.filter;
|
package com.gitee.sop.gatewaycommon.gateway.filter;
|
||||||
|
|
||||||
import com.gitee.sop.gatewaycommon.exception.ApiException;
|
import com.gitee.sop.gatewaycommon.exception.ApiException;
|
||||||
import com.gitee.sop.gatewaycommon.gateway.GatewayContext;
|
import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil;
|
||||||
import com.gitee.sop.gatewaycommon.param.ApiParam;
|
import com.gitee.sop.gatewaycommon.param.ApiParam;
|
||||||
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
|
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
|
||||||
import com.gitee.sop.gatewaycommon.validate.Validator;
|
import com.gitee.sop.gatewaycommon.validate.Validator;
|
||||||
@@ -29,7 +29,7 @@ public class ValidateFilter implements GlobalFilter, Ordered {
|
|||||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
// 解析参数
|
// 解析参数
|
||||||
ApiParam param = paramBuilder.build(exchange);
|
ApiParam param = paramBuilder.build(exchange);
|
||||||
GatewayContext.setApiParam(exchange, param);
|
ServerWebExchangeUtil.setApiParam(exchange, param);
|
||||||
// 验证操作,这里有负责验证签名参数
|
// 验证操作,这里有负责验证签名参数
|
||||||
try {
|
try {
|
||||||
validator.validate(param);
|
validator.validate(param);
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
package com.gitee.sop.gatewaycommon.gateway.param;
|
package com.gitee.sop.gatewaycommon.gateway.param;
|
||||||
|
|
||||||
import com.gitee.sop.gatewaycommon.gateway.GatewayContext;
|
import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil;
|
||||||
import com.gitee.sop.gatewaycommon.param.BaseParamBuilder;
|
import com.gitee.sop.gatewaycommon.param.BaseParamBuilder;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
@@ -13,8 +13,8 @@ import java.util.Map;
|
|||||||
public class GatewayParamBuilder extends BaseParamBuilder<ServerWebExchange> {
|
public class GatewayParamBuilder extends BaseParamBuilder<ServerWebExchange> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> buildRequestParams(ServerWebExchange exchange) {
|
public Map<String, ?> buildRequestParams(ServerWebExchange exchange) {
|
||||||
Map<String, String> params = GatewayContext.getRequestParams(exchange);
|
Map<String, ?> params = ServerWebExchangeUtil.getRequestParams(exchange);
|
||||||
return params == null ? Collections.emptyMap() : params;
|
return params == null ? Collections.emptyMap() : params;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package com.gitee.sop.gatewaycommon.gateway.route;
|
package com.gitee.sop.gatewaycommon.gateway.route;
|
||||||
|
|
||||||
import com.gitee.sop.gatewaycommon.bean.SopConstants;
|
import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil;
|
||||||
import com.gitee.sop.gatewaycommon.gateway.GatewayContext;
|
|
||||||
import com.gitee.sop.gatewaycommon.param.ParamNames;
|
import com.gitee.sop.gatewaycommon.param.ParamNames;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
|
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
|
||||||
@@ -45,14 +44,17 @@ public class NameVersionRoutePredicateFactory extends AbstractRoutePredicateFact
|
|||||||
public Predicate<ServerWebExchange> apply(Config config) {
|
public Predicate<ServerWebExchange> apply(Config config) {
|
||||||
|
|
||||||
return exchange -> {
|
return exchange -> {
|
||||||
Map<String, String> params = GatewayContext.getRequestParams(exchange);
|
Map<String, ?> params = ServerWebExchangeUtil.getRequestParams(exchange);
|
||||||
if (CollectionUtils.isEmpty(params)) {
|
if (CollectionUtils.isEmpty(params)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
String nameVersion = config.param;
|
String nameVersion = config.param;
|
||||||
String name = params.getOrDefault(ParamNames.API_NAME, SopConstants.UNKNOWN_METHOD);
|
Object name = params.get(ParamNames.API_NAME);
|
||||||
String version = params.getOrDefault(ParamNames.VERSION_NAME, "");
|
Object version = params.get(ParamNames.VERSION_NAME);
|
||||||
return (name + version).equals(nameVersion);
|
if (name == null || version == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return (name.toString() + version).equals(nameVersion);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -6,6 +6,7 @@ import com.gitee.sop.gatewaycommon.bean.BeanInitializer;
|
|||||||
import com.gitee.sop.gatewaycommon.bean.SpringContext;
|
import com.gitee.sop.gatewaycommon.bean.SpringContext;
|
||||||
import com.gitee.sop.gatewaycommon.limit.LimitManager;
|
import com.gitee.sop.gatewaycommon.limit.LimitManager;
|
||||||
import com.gitee.sop.gatewaycommon.message.ErrorFactory;
|
import com.gitee.sop.gatewaycommon.message.ErrorFactory;
|
||||||
|
import com.gitee.sop.gatewaycommon.param.ParameterFormatter;
|
||||||
import com.gitee.sop.gatewaycommon.secret.IsvManager;
|
import com.gitee.sop.gatewaycommon.secret.IsvManager;
|
||||||
import com.gitee.sop.gatewaycommon.session.SessionManager;
|
import com.gitee.sop.gatewaycommon.session.SessionManager;
|
||||||
import com.gitee.sop.gatewaycommon.validate.Validator;
|
import com.gitee.sop.gatewaycommon.validate.Validator;
|
||||||
@@ -93,6 +94,13 @@ public class AbstractConfiguration implements ApplicationContextAware {
|
|||||||
return ApiConfig.getInstance().getSessionManager();
|
return ApiConfig.getInstance().getSessionManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
ParameterFormatter sopParameterFormatter(){
|
||||||
|
return ApiConfig.getInstance().getParameterFormatter();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 跨域过滤器
|
* 跨域过滤器
|
||||||
*
|
*
|
||||||
|
@@ -7,7 +7,7 @@ import java.util.Map;
|
|||||||
*
|
*
|
||||||
* @author tanghc
|
* @author tanghc
|
||||||
*/
|
*/
|
||||||
public interface ParameterFormatter<T extends Map<String, Object>> {
|
public interface Formatter<T extends Map<String, Object>> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 参数格式化,即动态修改请求参数
|
* 参数格式化,即动态修改请求参数
|
@@ -0,0 +1,10 @@
|
|||||||
|
package com.gitee.sop.gatewaycommon.param;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.gitee.sop.gatewaycommon.manager.Formatter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author tanghc
|
||||||
|
*/
|
||||||
|
public interface ParameterFormatter extends Formatter<JSONObject> {
|
||||||
|
}
|
@@ -1,10 +1,8 @@
|
|||||||
package com.gitee.sop.gatewaycommon.zuul.configuration;
|
package com.gitee.sop.gatewaycommon.zuul.configuration;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
||||||
import com.gitee.sop.gatewaycommon.bean.ApiContext;
|
import com.gitee.sop.gatewaycommon.bean.ApiContext;
|
||||||
import com.gitee.sop.gatewaycommon.manager.AbstractConfiguration;
|
import com.gitee.sop.gatewaycommon.manager.AbstractConfiguration;
|
||||||
import com.gitee.sop.gatewaycommon.manager.ParameterFormatter;
|
|
||||||
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
|
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
|
||||||
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
|
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
|
||||||
import com.gitee.sop.gatewaycommon.zuul.filter.ErrorFilter;
|
import com.gitee.sop.gatewaycommon.zuul.filter.ErrorFilter;
|
||||||
@@ -47,12 +45,6 @@ public class BaseZuulConfiguration extends AbstractConfiguration {
|
|||||||
return ApiConfig.getInstance().getZuulParamBuilder();
|
return ApiConfig.getInstance().getZuulParamBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
ParameterFormatter<JSONObject> preParamFilter(){
|
|
||||||
return ApiConfig.getInstance().getZuulParameterFormatter();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 路由存储
|
* 路由存储
|
||||||
* @return
|
* @return
|
||||||
|
@@ -2,8 +2,8 @@ package com.gitee.sop.gatewaycommon.zuul.filter;
|
|||||||
|
|
||||||
import com.gitee.sop.gatewaycommon.param.ApiParam;
|
import com.gitee.sop.gatewaycommon.param.ApiParam;
|
||||||
import com.gitee.sop.gatewaycommon.param.ParamNames;
|
import com.gitee.sop.gatewaycommon.param.ParamNames;
|
||||||
|
import com.gitee.sop.gatewaycommon.param.ParameterFormatter;
|
||||||
import com.gitee.sop.gatewaycommon.zuul.ZuulContext;
|
import com.gitee.sop.gatewaycommon.zuul.ZuulContext;
|
||||||
import com.gitee.sop.gatewaycommon.zuul.param.ZuulParameterFormatter;
|
|
||||||
import com.gitee.sop.gatewaycommon.zuul.param.ZuulParameterUtil;
|
import com.gitee.sop.gatewaycommon.zuul.param.ZuulParameterUtil;
|
||||||
import com.netflix.zuul.context.RequestContext;
|
import com.netflix.zuul.context.RequestContext;
|
||||||
import com.netflix.zuul.exception.ZuulException;
|
import com.netflix.zuul.exception.ZuulException;
|
||||||
@@ -17,7 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
public class PreParameterFormatterFilter extends BaseZuulFilter {
|
public class PreParameterFormatterFilter extends BaseZuulFilter {
|
||||||
|
|
||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
private ZuulParameterFormatter zuulParameterFormatter;
|
private ParameterFormatter sopParameterFormatter;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected FilterType getFilterType() {
|
protected FilterType getFilterType() {
|
||||||
@@ -33,8 +33,8 @@ public class PreParameterFormatterFilter extends BaseZuulFilter {
|
|||||||
protected Object doRun(RequestContext requestContext) throws ZuulException {
|
protected Object doRun(RequestContext requestContext) throws ZuulException {
|
||||||
ApiParam apiParam = ZuulContext.getApiParam();
|
ApiParam apiParam = ZuulContext.getApiParam();
|
||||||
// 校验成功后进行参数转换
|
// 校验成功后进行参数转换
|
||||||
if (zuulParameterFormatter != null) {
|
if (sopParameterFormatter != null) {
|
||||||
ZuulParameterUtil.format(apiParam, zuulParameterFormatter::format);
|
ZuulParameterUtil.format(apiParam, sopParameterFormatter::format);
|
||||||
requestContext.addZuulRequestHeader(ParamNames.HEADER_VERSION_NAME, apiParam.fetchVersion());
|
requestContext.addZuulRequestHeader(ParamNames.HEADER_VERSION_NAME, apiParam.fetchVersion());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@@ -1,10 +0,0 @@
|
|||||||
package com.gitee.sop.gatewaycommon.zuul.param;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.gitee.sop.gatewaycommon.manager.ParameterFormatter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author tanghc
|
|
||||||
*/
|
|
||||||
public interface ZuulParameterFormatter extends ParameterFormatter<JSONObject> {
|
|
||||||
}
|
|
Reference in New Issue
Block a user