代码示例Sample for check sign

nodejs

const body = JSON.parse(request.data);
body.timestamp = Date.parse(new Date()); //增加时间戳
// 自然排序 start
const keys = Object.keys(body).sort();
const newObj = {};
for (var i = 0; i < keys.length; i++) {
if(keys[i] === ‘sign’) continue;
newObj[keys[i]] = body[keys[i]];
}
// 自然排序 end
const security = ‘109f29e84d0c62807579af2592f5959f’;
const sign= CryptoJS.MD5(JSON.stringify(newObj)+security).toString();
body.sign = sign

然后访问接口http.post(url, body)

PHP

$headers[‘content-type’] = ‘application/json; charset=utf-8’;
$data = []; //post请求参数数组
$data[‘timestamp’] = time(); //增加时间戳
ksort($data); // 自然排序
$security = ‘109f29e84d0c62807579af2592f5959f’;
$sign = md5(json_encode($data, JSON_BIGINT_AS_STRING | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . $security)

生成的签名加入请求体中
$data[‘sign’] = $sign;
然后访问接口
curl.post($url, $headers, $data);

java

CSDN代码参考

<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.27</version>
</dependency>

//使用了工具库https://doc.hutool.cn
import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;

import java.util.Date;
import java.util.SortedMap;
import java.util.TreeMap;

public class XffbbApiTest {

    public static void main(String[] args) {
        String url = "https://testxffbbapi.fbbship.com/wms_api/store/carrier";
        SortedMap<String, Object> sortedMap = new TreeMap<>();
        sortedMap.put("store_id", 1);
        sortedMap.put("target", "US");
        sortedMap.put("timestamp", new Date().getTime());
        String sortedStr = JSONUtil.toJsonStr(sortedMap);
        String sign = MD5.create().digestHex(sortedStr + "c7f5f9872dba43e04c1e27c4b587292b");
        sortedMap.put("sign", sign);
        String result = HttpRequest.post(url)
                .header("Access-Token", "31de663ab1e2e099")
                .body(JSONUtil.toJsonStr(sortedMap))
                .execute().body();
        System.out.println(result);
    }
}

作者:admin  创建时间:2024-01-30 11:17
最后编辑:admin  更新时间:2025-05-06 11:38