动态修改请求参数

1.x
tanghc 5 years ago
parent 36b5123ad7
commit 96e47407f0
  1. 33
      doc/docs/files/10111_动态修改请求参数.md
  2. 4
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java
  3. 7
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ParameterFormatter.java
  4. 11
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/configuration/BaseZuulConfiguration.java
  5. 7
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/BaseZuulFilter.java
  6. 42
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreParameterFormatterFilter.java
  7. 8
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreValidateFilter.java
  8. 10
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/param/ZuulParameterFormatter.java
  9. 67
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/param/ZuulParameterUtil.java

@ -0,0 +1,33 @@
# 动态修改请求参数
自1.14.0开始,zuul网关支持动态修改请求参数。即在网关修改客户端传递过来的参数,然后发送到微服务端。
```
客户端参数{"name": "jim"} --> zuul中修改为{"name": "Lucy"} --> 微服务端将收到{"name": "Lucy"}
```
使用场景:客户端请求参数经过加密,在网关解密后,再次发送明文参数给微服务端
- 如何使用
在网关springboot启动函数中添加如下代码
```java
public static void main(String[] args) {
ApiConfig.getInstance().setZuulParameterFormatter(requestParams -> {
// 获取biz_content
JSONObject jsonObject = requestParams.getJSONObject(ParamNames.BIZ_CONTENT_NAME);
// 修改biz_content中的值
jsonObject.put("name", "name修改了111");
jsonObject.put("remark", "remark修改了222");
// 重新设置biz_content
requestParams.put(ParamNames.BIZ_CONTENT_NAME, jsonObject);
});
SpringApplication.run(SopGatewayApplication.class, args);
}
```
其中requestParams是客户端传递过来的参数,直接修改其中的值即可。
更多参考:com.gitee.sop.gatewaycommon.zuul.filter.PreParameterFormatterFilter.java

@ -15,7 +15,6 @@ import com.gitee.sop.gatewaycommon.manager.EnvGrayManager;
import com.gitee.sop.gatewaycommon.manager.IPBlacklistManager;
import com.gitee.sop.gatewaycommon.manager.IsvRoutePermissionManager;
import com.gitee.sop.gatewaycommon.manager.LimitConfigManager;
import com.gitee.sop.gatewaycommon.manager.ParameterFormatter;
import com.gitee.sop.gatewaycommon.manager.RouteConfigManager;
import com.gitee.sop.gatewaycommon.manager.ServiceErrorManager;
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
@ -35,6 +34,7 @@ import com.gitee.sop.gatewaycommon.validate.Signer;
import com.gitee.sop.gatewaycommon.validate.Validator;
import com.gitee.sop.gatewaycommon.zuul.configuration.ZuulErrorController;
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.netflix.zuul.context.RequestContext;
import lombok.Data;
@ -150,7 +150,7 @@ public class ApiConfig {
*/
private ZuulErrorController zuulErrorController = new ZuulErrorController();
private ParameterFormatter<RequestContext> zuulParametersFormatter;
private ZuulParameterFormatter zuulParameterFormatter;
// -------- fields ---------

@ -1,19 +1,18 @@
package com.gitee.sop.gatewaycommon.manager;
import com.alibaba.fastjson.JSONObject;
import java.util.Map;
/**
* 参数格式化
*
* @author tanghc
*/
public interface ParameterFormatter<T> {
public interface ParameterFormatter<T extends Map<String, Object>> {
/**
* 参数格式化即动态修改请求参数
*
* @param requestParams 原始请求参数在此基础上追加或修改参数
* @param requestContext requestContext
*/
void format(JSONObject requestParams, T requestContext);
void format(T requestParams);
}

@ -1,5 +1,6 @@
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.ApiContext;
import com.gitee.sop.gatewaycommon.manager.AbstractConfiguration;
@ -11,6 +12,7 @@ import com.gitee.sop.gatewaycommon.zuul.filter.FormBodyWrapperFilterExt;
import com.gitee.sop.gatewaycommon.zuul.filter.PostResultFilter;
import com.gitee.sop.gatewaycommon.zuul.filter.PreHttpServletRequestWrapperFilter;
import com.gitee.sop.gatewaycommon.zuul.filter.PreLimitFilter;
import com.gitee.sop.gatewaycommon.zuul.filter.PreParameterFormatterFilter;
import com.gitee.sop.gatewaycommon.zuul.filter.PreValidateFilter;
import com.gitee.sop.gatewaycommon.zuul.filter.PreVersionDecisionFilter;
import com.gitee.sop.gatewaycommon.zuul.filter.Servlet30WrapperFilterExt;
@ -47,8 +49,8 @@ public class BaseZuulConfiguration extends AbstractConfiguration {
@Bean
@ConditionalOnMissingBean
ParameterFormatter<RequestContext> preParamFilter(){
return ApiConfig.getInstance().getZuulParametersFormatter();
ParameterFormatter<JSONObject> preParamFilter(){
return ApiConfig.getInstance().getZuulParameterFormatter();
}
/**
@ -111,6 +113,11 @@ public class BaseZuulConfiguration extends AbstractConfiguration {
return new PreValidateFilter();
}
@Bean
PreParameterFormatterFilter preParameterFormatterFilter() {
return new PreParameterFormatterFilter();
}
/**
* 开启限流
*/

@ -23,11 +23,14 @@ public abstract class BaseZuulFilter extends ZuulFilter {
/** 签名验证过滤 */
public static final int PRE_VALIDATE_FILTER_ORDER = -1000;
/** 参数格式化过滤器 */
public static final int PRE_PARAMETER_FORMATTER_FILTER_ORDER = PRE_VALIDATE_FILTER_ORDER + 1;
/** 权限验证过滤 */
public static final int PRE_ROUTE_PERMISSION_FILTER_ORDER = PRE_VALIDATE_FILTER_ORDER + 1;
public static final int PRE_ROUTE_PERMISSION_FILTER_ORDER = PRE_VALIDATE_FILTER_ORDER + 100;
/** 限流过滤 */
public static final int PRE_LIMIT_FILTER_ORDER = PRE_ROUTE_PERMISSION_FILTER_ORDER + 1;
public static final int PRE_LIMIT_FILTER_ORDER = PRE_ROUTE_PERMISSION_FILTER_ORDER + 100;
private Integer filterOrder;

@ -0,0 +1,42 @@
package com.gitee.sop.gatewaycommon.zuul.filter;
import com.gitee.sop.gatewaycommon.param.ApiParam;
import com.gitee.sop.gatewaycommon.param.ParamNames;
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.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 参数格式化过滤器动态修改参数此过滤器放在前面校验后面
*
* @author tanghc
*/
public class PreParameterFormatterFilter extends BaseZuulFilter {
@Autowired(required = false)
private ZuulParameterFormatter zuulParameterFormatter;
@Override
protected FilterType getFilterType() {
return FilterType.PRE;
}
@Override
protected int getFilterOrder() {
return PRE_PARAMETER_FORMATTER_FILTER_ORDER;
}
@Override
protected Object doRun(RequestContext requestContext) throws ZuulException {
ApiParam apiParam = ZuulContext.getApiParam();
// 校验成功后进行参数转换
if (zuulParameterFormatter != null) {
ZuulParameterUtil.format(apiParam, zuulParameterFormatter::format);
requestContext.addZuulRequestHeader(ParamNames.HEADER_VERSION_NAME, apiParam.fetchVersion());
}
return null;
}
}

@ -1,7 +1,6 @@
package com.gitee.sop.gatewaycommon.zuul.filter;
import com.gitee.sop.gatewaycommon.exception.ApiException;
import com.gitee.sop.gatewaycommon.manager.ParameterFormatter;
import com.gitee.sop.gatewaycommon.param.ApiParam;
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
import com.gitee.sop.gatewaycommon.validate.Validator;
@ -20,9 +19,6 @@ public class PreValidateFilter extends BaseZuulFilter {
@Autowired
private ParamBuilder<RequestContext> paramBuilder;
@Autowired(required = false)
private ParameterFormatter<RequestContext> parameterFormatter;
@Autowired
private Validator validator;
@ -44,10 +40,6 @@ public class PreValidateFilter extends BaseZuulFilter {
// 验证操作,这里有负责验证签名参数
try {
validator.validate(param);
// 校验成功后进行参数转换
if (parameterFormatter != null) {
parameterFormatter.format(param, requestContext);
}
} catch (ApiException e) {
log.error("验证失败,ip:{}, params:{}", param.fetchIp(), param.toJSONString(), e);
throw e;

@ -0,0 +1,10 @@
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> {
}

@ -1,13 +1,10 @@
package com.gitee.sop.gatewaycommon.zuul.param;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gitee.sop.gatewaycommon.bean.SopConstants;
import com.gitee.sop.gatewaycommon.manager.ParameterFormatter;
import com.gitee.sop.gatewaycommon.param.ApiParam;
import com.gitee.sop.gatewaycommon.util.RequestUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.netflix.zuul.http.HttpServletRequestWrapper;
import com.netflix.zuul.http.ServletInputStreamWrapper;
import lombok.extern.slf4j.Slf4j;
@ -19,6 +16,7 @@ import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import javax.servlet.ServletInputStream;
@ -29,30 +27,55 @@ import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;
/**
* zuul参数工具
* @author tanghc
*/
@Slf4j
public abstract class BaseParameterFormatter implements ParameterFormatter<RequestContext> {
public class ZuulParameterUtil {
private FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
private static FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
public void formatParams(ApiParam apiParam, RequestContext requestContext) throws ZuulException {
this.format(apiParam, requestContext);
/**
* 格式化参数
* @param apiParam 请求的参数
* @param consumer 修改参数
* @param <T> 参数类型
*/
public static <T extends Map<String, Object>> void format(T apiParam, Consumer<T> consumer) {
RequestContext requestContext = RequestContext.getCurrentContext();
consumer.accept(apiParam);
HttpServletRequest request = requestContext.getRequest();
String contentType = request.getContentType();
if (StringUtils.containsIgnoreCase(contentType, MediaType.APPLICATION_JSON_VALUE)) {
byte[] bytes = apiParam.toJSONString().getBytes(StandardCharsets.UTF_8);
String json = (apiParam instanceof JSONObject) ?
((JSONObject) apiParam).toJSONString()
: JSON.toJSONString(apiParam);
byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
requestContext.setRequest(new ChangeParamsHttpServletRequestWrapper(request, bytes));
} else if(StringUtils.containsIgnoreCase(contentType, MediaType.APPLICATION_FORM_URLENCODED_VALUE)) {
List<String> list = Lists.newArrayList();
List<String> list = new ArrayList<>(apiParam.size());
try {
for (Map.Entry<String, Object> entry : apiParam.entrySet()) {
list.add(entry.getKey() + "=" + URLEncoder.encode(String.valueOf(entry.getValue()), SopConstants.UTF8));
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof Collection) {
Collection collection = (Collection) value;
for (Object el : collection) {
list.add(key + "=" + URLEncoder.encode(String.valueOf(el), SopConstants.UTF8));
}
} else {
list.add(key + "=" + URLEncoder.encode(String.valueOf(value), SopConstants.UTF8));
}
}
} catch (UnsupportedEncodingException e) {
log.error("字符集不支持", e);
@ -64,12 +87,19 @@ public abstract class BaseParameterFormatter implements ParameterFormatter<Reque
FormHttpOutputMessage outputMessage = new FormHttpOutputMessage();
try {
// 转成MultipartRequest
if (!(request instanceof MultipartHttpServletRequest)) {
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getServletContext());
request = commonsMultipartResolver.resolveMultipart(request);
}
// 重写新的值
MultiValueMap<String, Object> builder = RequestContentDataExtractor.extract(request);
for (Map.Entry<String, Object> entry : apiParam.entrySet()) {
builder.put(entry.getKey(), Collections.singletonList(String.valueOf(entry.getValue())));
Object value = entry.getValue();
if (value instanceof List) {
builder.put(entry.getKey(), (List)value);
} else {
builder.put(entry.getKey(), Collections.singletonList(String.valueOf(value)));
}
}
MediaType mediaType = MediaType.valueOf(request.getContentType());
// 将字段以及上传文件重写写入到流中
@ -84,9 +114,15 @@ public abstract class BaseParameterFormatter implements ParameterFormatter<Reque
log.error("修改上传文件请求参数失败, apiParam:{}", apiParam, e);
}
} else if(HttpMethod.GET.name().equalsIgnoreCase(request.getMethod())) {
Map<String, List<String>> newParams = Maps.newHashMap();
Map<String, List<String>> newParams = new HashMap<>();
for (Map.Entry<String, Object> entry : apiParam.entrySet()) {
newParams.put(entry.getKey(), Collections.singletonList(String.valueOf(entry.getValue())));
Object value = entry.getValue();
if (value instanceof List) {
List<String> valueList = ((List<?>) value).stream().map(String::valueOf).collect(Collectors.toList());
newParams.put(entry.getKey(), valueList);
} else {
newParams.put(entry.getKey(), Collections.singletonList(String.valueOf(value)));
}
}
requestContext.setRequestQueryParams(newParams);
}
@ -143,4 +179,5 @@ public abstract class BaseParameterFormatter implements ParameterFormatter<Reque
return data.length;
}
}
}
Loading…
Cancel
Save