适用于Ewei Open Api的Sign计算脚本

为方便在Postman中测试易维开放平台中需要sign参数相关的接口,写了两个Pre-Request-Script脚本,用于在Postman中动态计算签名。

文档:http://api.ewei.com/index#/index/developer_read/join_guide

##

v1 版本相关API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 前置处理器:计算请求签名(OpenV1)
var _app_secret = pm.environment.get("provider_app_secret");
var _time_open_v1 = (new Date()).valueOf();
var _request_method = request.method;
var _request_url_no_query_string = getRequestUrlWithNoQueryString();
var _request_body = pm.request.body.raw;

var _params = _request_method
+ getStringWithPopParameters(_request_url_no_query_string)
+ ("_app_secret=" + _app_secret)
+ ("_timestamp=" + _time_open_v1)
+ getQueryStringWithNoSplitter()
+ (_request_body ? ("requestBody=" + _request_body) : "");
console.log("_params = " + _params);

var _url_encoded_params = encodeURIComponent(_params);
console.log("_url_encoded_params = " + _url_encoded_params);

var _lower_url_encoded_params = _url_encoded_params.toLowerCase();
console.log("_lower_url_encoded_params = " + _url_encoded_params);

var _sign_open_v1 = CryptoJS.MD5(_lower_url_encoded_params).toString();
console.log("_sign_open_v1 = " + _sign_open_v1);

pm.environment.set("_time_open_v1", _time_open_v1);
pm.environment.set("_sign_open_v1", _sign_open_v1);

function getQueryStringWithNoSplitter() {
var request_url = request.url;
var query_string = request_url.substring(request_url.indexOf('?') + 1);
var query_string_no_splitter = query_string.replace("&","");
return getStringWithPopParameters(query_string_no_splitter);
}

function getRequestUrlWithNoQueryString() {
var request_url = request.url;
return request_url.substring(0, request_url.indexOf('?'));
}

function getStringWithPopParameters(str) {
var variableArray = extractPmVariables(str);
for (var i = 0; i < variableArray.length; i++) {
var va = variableArray[i];
if (va.variableValue) {
str = str.replace(va.variableKey, va.variableValue);
}
}
return str;
}

function extractPmVariables(requestUrl) {
var variableArray = [];
var variables = requestUrl.match(/{{[A-Za-z0-9-\$_]+}}/g);
if (variables) {
for (var i = 0; i < variables.length; i++) {
var variableKey = variables[i];
var variableName = variableKey.replace("{{","").replace("}}","");
var variableValue = pm.environment.get(variableName);
variableArray.push({
"variableKey": variableKey,
"variableName": variableName,
"variableValue": variableValue
});
}
}
return variableArray;
}

Ewei Dubbo自定义Http协议返回值过滤器

公司将Dubbo又封装了一层,通过注解直接暴露Service接口,在序列化返回结果时,简单粗暴的将实体的所有非空属性全部序列化出来了,接口的返回体相当庞大,很是浪费资源。

核心实现

使用FastJson的com.alibaba.fastjson.serializer.PropertyFilter,在序列化时,排除相关的属性,核心代码如下:

1
2
3
4
5
6
7
8
9
10
PropertyFilter profilter = new PropertyFilter(){
@Override
public boolean apply(Object object, String name, Object value) {
if(name.equalsIgnoreCase("password")){
return false; // 排除password 属性
}
return true;
}
};
serializer.addFilter(profilter);

V2接口基于Dubbo相关协议封装(JavaDoc:http://doc.ewei.ren/budo-dubbo-protocol-http/),在序列化接口返回信息时,没有做相关的配置;

其核心序列化逻辑在org.budo.dubbo.protocol.http.view.render.ViewRendercom.ewei.common.dubbo.http.view.render.EweiDubboHttpApiJsonViewRender实现类中,见下图:



现只需要在序列化之前,添加相关的PropertyFilter即可。

1
serializer.addFilter(new XxxPropertyFilter());
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×