登录

您的位置 : 文档中心 -> API信息

加密规则

签名规则:根据参数名称将你的应用级别和系统级别参数对象里的请求参数(排除sign、file参数之外的所有参数)
按照字母先后顺序排序:key + value .... key + value并且按key做的升序排列, value无需编码。(如果value为null的参数
,则不加入签名里,key排序区分大小写)例如:将foo=1,bar=2,baz=3,roy=null经过排序并且非空处理后为bar=2,baz=3,foo=1
参数名和参数值链接后,得到拼装字符串bar2baz3foo1 c#示例:param.Sort((a, b) => String.Compare(a.Key,b.Key,false));
不忽略大小写 java示例:Arrays.sort(String[] a);系统支持MD5加密方式md5:将appsecret拼接到参数字符串头进行md5加密,
再转化成小写,格式是:md5(appsecretkey1value1key2value2... ),此值即为签名参数sign的值。注:签名值需要的是32位小写MD5值。
appkey与appsecret由调用方开发人员在线申请获得,服务端根据调用方传递的appkey获取数据库中对应的appsecret做签名验证,
如果服务端的签名值与调用方传递的sign的签名值相等,则允许调用方调用服务端接口。


 

签名实现java代码示例

package com.fesco.fws.medicalinsurance.service.impl;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class GetSignDemo{

  //使用字符串方式加密
  public static void main(String[] args) throws Exception{
    //Demo
    //方法一:获取签名的字符串
    String str="appkey="开发者在线注册获取的appkey"&sign=aaa91a2018d08395e6f35ac6ac97e2ee&format=json&timeStamp=1490349318962&methodName=csmgr.createOfferCommon&jsonList={\"empName\":\"王小二\",\"exactCerType\":1,\"idCardNo\":\"1111101112\",\"country\":156,\"isForeigner\":0,\"groupBusiCustTag\":\"ofo_001\",\"offerAreaId\":\"1415677\",\"repoDate\":\"2018-05-01\",\"orderBeginDate\":\"2018-05-01\",\"isgraduateRecv\":0,\"isUseCb\":0,\"isScTax\":0,\"isBjgradu\":3,\"isForeignerTax\":0,\"isgraduate\":0,\"payFeesPerType\":\"13\",\"insAddType\":\"1\",\"contractType\":3,\"conTel\":\"18688886666\",\"birthDate\":\"1982-01-01\",\"empInsideNo\":\"000700002\",\"csOfferDetAccuDtoList\":[{\"beginDate\":\"2018-05-01\",\"execuAreaId\":\"1415677\",\"declWage\":10000,\"payWay\":\"2\"}]}";
    //密钥
    String secret =  "开发者在线注册获取的appsecret";
    String sign = GetSign.getSign(str, secret);
    System.out.println(sign);

    //方法二:使用对象的方式获取
    SignModel signModel = new SignModel();
    signModel.setAppkey("YellowBike");
    signModel.setFormat("json");
    signModel.setTimeStamp(String.valueOf("1490349318962"));
//    signModel.setTimeStamp(String.valueOf(System.currentTimeMillis()));
    signModel.setMethodName("csmgr.createOfferCommon");
    signModel.setJsonList("{\"empName\":\"王小二\",\"exactCerType\":1,\"idCardNo\":\"1111101112\",\"country\":156,\"isForeigner\":0,\"groupBusiCustTag\":\"ofo_001\",\"offerAreaId\":\"1415677\",\"repoDate\":\"2018-05-01\",\"orderBeginDate\":\"2018-05-01\",\"isgraduateRecv\":0,\"isUseCb\":0,\"isScTax\":0,\"isBjgradu\":3,\"isForeignerTax\":0,\"isgraduate\":0,\"payFeesPerType\":\"13\",\"insAddType\":\"1\",\"contractType\":3,\"conTel\":\"18688886666\",\"birthDate\":\"1982-01-01\",\"empInsideNo\":\"000700002\",\"csOfferDetAccuDtoList\":[{\"beginDate\":\"2018-05-01\",\"execuAreaId\":\"1415677\",\"declWage\":10000,\"payWay\":\"2\"}]}");
    String sign1 = GetSign.getSign(signModel, secret);
    System.out.println(sign1);
  }

}
class GetSign {

  //获取签名
  public static String getSign(String str, String secret) {
    //字符串转map
    Map param = convert(str);
    //校验必填项
    checkRequiredParameters(param);
    //获取签名
    param.remove("sign");
    String sign = null ;
    try {
      //添加签名参数
      if(param == null ) {
        param = new HashMap();
      }
      param.put("appkey", param.get("appkey"));
      if(!"".equals("json") && "json" != null && !"null".equals("json")) {
        param.put("format", "json");
      }
      param.remove("thisIsServiceJobSign");
      //对map签名
      sign = signingObjectByMd5(param , secret);

    } catch (Exception e) {
      throw new RuntimeException("AccountServiceImpl getSign signingObjectByMd5 : 对象签名出现异常,签名失败 :",
          e);
    }
    return sign;
  }
  public static String getSign(Object obj, String secret) {
    Map param = null;
    try {
      param = convertBean(obj);
    } catch (IntrospectionException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }
    //字符串转map
//    Map param = convert(str);
    //校验必填项
    checkRequiredParameters(param);
    //获取签名
    param.remove("sign");
    String sign = null ;
    try {
      //添加签名参数
      if(param == null ) {
        param = new HashMap();
      }
      param.put("appkey", param.get("appkey"));
      if(!"".equals("json") && "json" != null && !"null".equals("json")) {
        param.put("format", "json");
      }
      param.remove("thisIsServiceJobSign");
      //对map签名
      sign = signingObjectByMd5(param , secret);

    } catch (Exception e) {
      throw new RuntimeException("AccountServiceImpl getSign signingObjectByMd5 : 对象签名出现异常,签名失败 :",
          e);
    }
    return sign;
  }
  //MD5加密
  public static String signingObjectByMd5(Map paramMap, String args) throws Exception {
    String[] keyArray = (String[])paramMap.keySet().toArray(new String[0]);
    Arrays.sort(keyArray);
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(args);
    String[] arr$ = keyArray;
    int len$ = keyArray.length;

    for(int i$ = 0; i$ < len$; ++i$) {
      String key = arr$[i$];
      if (paramMap.get(key) != null && !"".equals(paramMap.get(key))) {
        stringBuffer.append(key).append(String.valueOf(paramMap.get(key)));
      }
    }

    String codes = stringBuffer.toString();
    System.out.println(codes);
    String sign = "";

    try {
      sign = MD5Util.encrypt(codes);
    } catch (Exception var8) {
      var8.printStackTrace();
    }

    return sign;
  }

  //将字符串转换成map对象
  public static Map convert(String params) {
    Map res = new HashMap();
    String[] param = params.split("&");
    for (String kv : param) {
      String[] tmp = kv.split("=",2);
      res.put(tmp[0], tmp[1].toString());
    }
    return res;
  }
  //校验必填项
  private static void checkRequiredParameters(Map map) throws RuntimeException {
    if ( map.get("timeStamp") == null || "".equals(map.get("appKey")) ) {
      throw new RuntimeException("缺少必填项:appKey");
    }
    if ( map.get("timeStamp") == null || "".equals(map.get("timeStamp")) ) {
      throw new RuntimeException("缺少必填项:timeStamp");
    }
    if ( map.get("methodName") == null || "".equals(map.get("methodName")) ) {
      throw new RuntimeException("缺少必填项:methodName");
    }
    if ( map.get("format") == null || "".equals(map.get("format")) ) {
      throw new RuntimeException("缺少必填项:format");
    }
  }
  //将java对象转换成Map
  public static Map convertBean(Object bean) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Class type = bean.getClass();
    Map returnMap = new HashMap();
    BeanInfo beanInfo = Introspector.getBeanInfo(type);

    PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
    for (int i = 0; i< propertyDescriptors.length; i++) {
      PropertyDescriptor descriptor = propertyDescriptors[i];
      String propertyName = descriptor.getName();
      if (!propertyName.equals("class")) {
        Method readMethod = descriptor.getReadMethod();
        Object result = readMethod.invoke(bean, new Object[0]);
        if (result != null) {
          returnMap.put(propertyName, result.toString());
        } else {
          returnMap.put(propertyName, "");
        }
      }
    }
    return returnMap;
  }
}

//MD5加密工具
class MD5Util {

  public MD5Util() {
  }

  public static String encrypt(String str) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    md5.update(str.getBytes("utf-8"));
    return byte2hex(md5.digest());
  }

  public static String byte2hex(byte[] b) {
    String hs = "";
    String stmp = "";

    for (int n = 0; n < b.length; ++n) {
      stmp = Integer.toHexString(b[n] & 255);
      if (stmp.length() == 1) {
        hs = hs + "0" + stmp;
      } else {
        hs = hs + stmp;
      }

      if (n < b.length - 1) {
        hs = hs + "";
      }
    }

    return hs;
  }

}

class SignModel{
  private String appkey;
  private String timeStamp;
  private String sign;
  private String methodName;
  private String format;
  private String appsecret;
  private Object jsonList;

  public String getAppkey() {
    return appkey;
  }

  public SignModel setAppkey(String appkey) {
    this.appkey = appkey;
    return this;
  }

  public String getTimeStamp() {
    return timeStamp;
  }

  public SignModel setTimeStamp(String timeStamp) {
    this.timeStamp = timeStamp;
    return this;
  }

  public String getSign() {
    return sign;
  }

  public SignModel setSign(String sign) {
    this.sign = sign;
    return this;
  }

  public String getMethodName() {
    return methodName;
  }

  public SignModel setMethodName(String methodName) {
    this.methodName = methodName;
    return this;
  }

  public String getFormat() {
    return format;
  }

  public SignModel setFormat(String format) {
    this.format = format;
    return this;
  }

  public String getAppsecret() {
    return appsecret;
  }

  public SignModel setAppsecret(String appsecret) {
    this.appsecret = appsecret;
    return this;
  }

  public Object getJsonList() {
    return jsonList;
  }

  public SignModel setJsonList(Object jsonList) {
    this.jsonList = jsonList;
    return this;
  }
}

 

联系我们

400-8000-800

产品咨询:010-85692930

FESCO官方

FESCO服务