前缀URL:https://service.kecq.com
| 字段 | 取值 | 是否必须 | 描述 |
|---|---|---|---|
| ak | 是 | 系统分配的的appKey | |
| time | 是 | 当前时间,格式yyyyMMddHHmmss 不能与服务器时间相差太大 | |
| guid | 是 | 32-50位的随机值 | |
| token | 否 | 除了登录接口基本都需要 | |
| ip | 否 | IP限制(暂时未启用) | |
| sign | 是 | 用系统分配的密钥进行URL签名 |
以上参数用GET方式传输 如:https://service.kecq.com/User/Login.ashx?ak=afbf3d192908477d9e24b3e351bc4ebe&time=20140827203145&ip=8.8.8.8&sign=xxxxxxxx
签名方法:
取请求url的相对路径 以/开头 不能包含 time ak sign guid token参数
对以上url 加上 ?time=yyyyMMddHHmmss&guid=xxxxxxx&token=xxxx 或者 &time=yyyyMMddHHmmss&guid=xxxxxxx&token=xxxx token一般除了用户接口都需要
再对以上url 用密钥(appSecret)进行HMACSHA1 签名, 再进行Base64编码, 再对Base64编码后的字符串进行Url编码 就得到了sign
最后请求的url 变成 原url?time=yyyyMMddHHmmss&guid=xxxxxxx&&token=xxxx&ak=xxxxx&sign=xxxx ak为公钥(appKey),如果原url带有参数?time=变成&time=
举例 请求 URL /api/Article/QueryList
加上 time=20171220234953&guid=123
String s1= HMACSHA1("/api/Article/QueryList?time=20171220234953&guid=123&token=xxxx","密钥");
String s2=Base64(s1);
String sign=UrlEncode(s2);
最后url变成/api/Article/QueryList?time=20171220234953&guid=123&token=xxxx&ak=56d1e3587cd84a9baa8c2430cdee211c&sign=hK+CyNqyIEtPprdI6Ty8vFdTQ/0=
公共返回示例:{"code":"-1","message":"登录名或密码错误"}
结果说明:code代表返回的代码,大于0表示成功,小于0表示失败,message代表返回结果描述
appKey:
AppSecret:
Android 签名代码示例:
private static final String HMAC_SHA1 = "HmacSHA1";
public static String Sign(String dataString,String keyString) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
byte[] key=keyString.getBytes("utf-8");
byte[] data=dataString.getBytes("utf-8");
SecretKeySpec signingKey = new SecretKeySpec(key, HMAC_SHA1);
Mac mac = Mac.getInstance(HMAC_SHA1);
mac.init(signingKey);
byte[] signByte = mac.doFinal(data);
String sign= Base64.encodeToString(signByte,Base64.NO_WRAP);
String result=java.net.URLEncoder.encode(sign,"UTF-8");
return result;
}
/**
* 对URL进行签名并返回签名后的URL url必须是以 /开头的相对路径 并且不得包含 ak time sign参数
* 结果是对原url加上 time=xxxxx&ak=xxxxx&sign=xxxx
*/
public static String SignUrl(String url,String ak, String sk)
{
try {
if(!url.startsWith("/")){
LogHelper.e("签名URL必须以/开头");
}
SimpleDateFormat si = new SimpleDateFormat("yyyyMMddHHmmss");
si.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
String time= si.format(new Date());
String pre="&";
if(!url.contains("?")){
pre="?";
}
url=url+pre+"time="+time+"&guid="+ UUID.randomUUID().toString().replace("-","")+"&token=xxxxxx";
url=url+"&ak="+ak+"&sign="+Sign(url,sk);
return url;
}
catch(Exception e)
{
LogHelper.DoException("SignHelper签名异常",e);
return null;
}
}