美团北极星 洗涤行业对接

北栀
2023-03-10 / 3 评论 / 323 阅读 / 正在检测是否收录...

美团北极星 洗涤行业对接
美团洗衣行业对接 java代码
签名不一致 sign值不一致
中文乱码导致的 本地测试没问题 线上tomcat有问题
需要先去下载java SDK
微信截图_20230310143807.png
SDK 版本:

    public static String generateSign(GenerateSignRequest generateSignRequest) {
    if (generateSignRequest == null) {
        throw new IllegalArgumentException("generateSign generateSignRequest is null");
    } else {
        Map<String, String> params = generateSignRequest.getParams();
        AssertUtils.checkArgument(params != null && !params.isEmpty(), "generateSign apiRequest is null,generateSignRequest:" + generateSignRequest);
        List<String> keys = new ArrayList();
        Iterator var3 = params.entrySet().iterator();

        while(var3.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry)var3.next();
            if (org.apache.commons.lang.StringUtils.isNotEmpty((String)entry.getValue())) {
                keys.add(entry.getKey());
            }
        }

        Collections.sort(keys);
        StringBuilder sb = new StringBuilder();
        if (org.apache.commons.lang.StringUtils.isNotEmpty(generateSignRequest.getAppSecret())) {
            sb.append(generateSignRequest.getAppSecret());
        }

        Iterator var8 = keys.iterator();

        String sign;
        while(var8.hasNext()) {
            sign = (String)var8.next();
            sb.append(sign).append(((String)params.get(sign)).trim());
        }

        if (org.apache.commons.lang.StringUtils.isNotEmpty(generateSignRequest.getAppSecret())) {
            sb.append(generateSignRequest.getAppSecret());
        }

        String encryptionKey = sb.toString().trim();
        if (generateSignRequest.getSignMethod().equals(SignMethodEnum.MD5.getType())) {
            try {
                sign =genMd5(encryptionKey);
                return sign;
            } catch (Exception var6) {
                throw new RuntimeException(var6.getMessage());
            }
        } else {
            return "";
        }
    }
}


public static String genMd5(String info) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    byte[] infoBytes = info.getBytes("UTF-8");
    md5.update(infoBytes);
    byte[] sign = md5.digest();
    return byteArrayToHex(sign);
}

public static String byteArrayToHex(byte[] bytes) {
    StringBuilder sign = new StringBuilder();

    for(int i = 0; i < bytes.length; ++i) {
        String hex = Integer.toHexString(bytes[i] & 255);
        if (hex.length() == 1) {
            sign.append("0");
        }

        sign.append(hex.toLowerCase());
    }

    return sign.toString();
}

java代码

public static String generateSign(Map<String, String> params, String appSecret, String signMethod) {         
// 第一步:参数排序
List<String> keys = Lists.newArrayList();
for (Map.Entry<String, String> entry : params.entrySet()) {
    if (StringUtils.isNotEmpty(entry.getValue())) {
        keys.add(entry.getKey());
    }
}
Collections.sort(keys);         
// 第二步:把所有参数名和参数值串在一起
StringBuilder sb = new StringBuilder();
if (StringUtils.isNotEmpty(appSecret)) {
    sb.append(appSecret);
}
for (String key : keys) {
    sb.append(key).append(params.get(key).trim());
}
if (StringUtils.isNotEmpty(appSecret)) {
    sb.append(appSecret);
}
String encryptionKey = sb.toString().trim();         
// 第三步:加签
if (signMethod.equals("MD5")) {
    try {
        String sign = genMd5(encryptionKey);
        return sign;
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}else{             
    //开发者暂不需支持,支持MD5即可
    return "";
}

}

public static String genMd5(String info) throws NoSuchAlgorithmException, UnsupportedEncodingException {

MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] infoBytes = info.getBytes();
md5.update(infoBytes);
byte[] sign = md5.digest();
return byteArrayToHex(sign);

}

public static String byteArrayToHex(byte[] bytes) {

StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
    String hex = Integer.toHexString(bytes[i] & 0xFF);
    if (hex.length() == 1) {
        sign.append("0");
    }
    sign.append(hex.toLowerCase());
}
return sign.toString();

}

0

评论 (3)

取消
  1. 头像
    " rel="external nofollow">知名2009
    Windows 10 · Google Chrome

    表评论2009

    回复
  2. 头像
    3
    Windows 10 · Google Chrome

    你能验券吗,我接口都不通啊 报什么验证渠道不合法

    回复
    1. 头像
      nankui 作者
      Windows 10 · Google Chrome
      @ 3

      直接调用官方sdk就可以

      回复