注册  |  登录
签名算法简介
使用规则:
1. 将请求参数按参数名升序排序;
2. 按请求参数名及参数值相互连接组成一个字符串:<paramName1>=<paramValue1>&<paramName2>=<paramValue2>...;
3. 将应用密钥分别添加到以上请求参数串的头部和尾部:<secret><请求参数字符串><secret>;
4. 对该字符串进行MD5,MD5后的字符串即是这些请求参数对应的签名;
5. 该签名值使用sign参数一起和其它请求参数一起发送给服务开放平台。
请求示例:
          
            package com.ipr.justice.web;
            import com.alibaba.fastjson.JSON;
            import com.ipr.justice.Tester;
            import org.apache.commons.codec.digest.DigestUtils;
            import org.apache.commons.lang3.StringUtils;
            import org.junit.Test;
            import org.springframework.web.multipart.MultipartFile;

            import java.math.BigInteger;
            import java.security.MessageDigest;
            import java.text.SimpleDateFormat;
            import java.util.*;

            public class OriginalAuthorControllerTest extends Tester {


                @Test
                public void testGenSign() throws Exception {

                    String clientId = "系统分配的clientId";
                    String clientSecret = "系统分配的clientSecret";
                    //参数
                    Map<String, Object> param = new HashMap<String, Object>();
                    //系统参数
                    param.put("clientId", clientId);
                    param.put("timestamp", getTime());
                    //业务参数
                    param.put("authorName","");
                    param.put("idCard","");
                    param.put("belong","");
                    param.put("fileName","");
                    param.put("fileDesc","");
                    param.put("fileType","");
                    //上传文件必须添加文件md5值,否则文件验证不通过
                    param.put("fileMd5", messageDigest(null));

                    String sign = buildSign(param, clientSecret);
                    param.put("sign", sign);

                    System.out.println("=====请求数据=====");
                    System.out.println(JSON.toJSON(param));

                }

                private static String messageDigest(MultipartFile file){
                    MessageDigest md = null;
                    try {
                        md = MessageDigest.getInstance("MD5");
                        return new BigInteger(1, md.digest(file.getBytes())).toString(16);
                    } catch (Exception e) {
                        return null;
                    }
                }

                private String buildSign(Map<String, Object> param, String secret) {
                    Set<String> keySet = param.keySet();
                    List<String> keys = new ArrayList<String>(keySet);
                    Collections.sort(keys);//排序

                    StringBuilder sb = new StringBuilder();
                    for (String key : keys) {
                        sb.append(key).append("=").append(param.get(key)).append("&");//拼接字符串
                    }
                    String linkString = sb.toString();
                    linkString = StringUtils.substring(linkString, 0, linkString.length() - 1);//去除最后一个'&'
                    String sign = DigestUtils.md5Hex(secret + linkString + secret);//混合密钥md5
                    return sign;
                }


                private String getTime() {
                    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                }
            }