parent
36b5123ad7
commit
96e47407f0
@ -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 |
@ -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); |
||||
} |
||||
|
@ -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; |
||||
} |
||||
} |
@ -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> { |
||||
} |
Loading…
Reference in new issue