2.3 生成签名
签名是为了让请求合法,共分为五步:
第一步:请求参数
- 请求参数包括系统参数和业务参数,不要遗漏
- 参数Key中不能包含最终要生成签名Key(也就是说Signature这个Key是关键字)
参考代码如下
- [backcolor=transparent]String[backcolor=transparent] accessKeyId [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"testId"[backcolor=transparent];
- [backcolor=transparent]String[backcolor=transparent] accessSecret [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"testSecret"[backcolor=transparent];
- [backcolor=transparent]java[backcolor=transparent].[backcolor=transparent]text[backcolor=transparent].[backcolor=transparent]SimpleDateFormat[backcolor=transparent] df [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]text[backcolor=transparent].[backcolor=transparent]SimpleDateFormat[backcolor=transparent]([backcolor=transparent]"yyyy-MM-dd'T'HH:mm:ss'Z'"[backcolor=transparent]);
- [backcolor=transparent]df[backcolor=transparent].[backcolor=transparent]setTimeZone[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]SimpleTimeZone[backcolor=transparent]([backcolor=transparent]0[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"GMT"[backcolor=transparent]));[backcolor=transparent]// 这里一定要设置GMT时区
- [backcolor=transparent]java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]Map[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]>[backcolor=transparent] paras [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]HashMap[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]>();
- [backcolor=transparent]// 1. 系统参数
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignatureMethod"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"HMAC-SHA1"[backcolor=transparent]);
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignatureNonce"[backcolor=transparent],[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]UUID[backcolor=transparent].[backcolor=transparent]randomUUID[backcolor=transparent]().[backcolor=transparent]toString[backcolor=transparent]());
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"AccessKeyId"[backcolor=transparent],[backcolor=transparent] accessKeyId[backcolor=transparent]);
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignatureVersion"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"1.0"[backcolor=transparent]);
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Timestamp"[backcolor=transparent],[backcolor=transparent] df[backcolor=transparent].[backcolor=transparent]format[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]Date[backcolor=transparent]()));
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Format"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"XML"[backcolor=transparent]);
- [backcolor=transparent]// 2. 业务API参数
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Action"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"SendSms"[backcolor=transparent]);
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Version"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"2017-05-25"[backcolor=transparent]);
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"RegionId"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"cn-hangzhou"[backcolor=transparent]);
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"PhoneNumbers"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"15300000001"[backcolor=transparent]);
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignName"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"阿里云短信测试专用"[backcolor=transparent]);
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"TemplateParam"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"{\"customer\":\"test\"}"[backcolor=transparent]);
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"TemplateCode"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"SMS_71390007"[backcolor=transparent]);
- [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"OutId"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"123"[backcolor=transparent]);
- [backcolor=transparent]// 3. 去除签名关键字Key
- [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]containsKey[backcolor=transparent]([backcolor=transparent]"Signature"[backcolor=transparent]))
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]remove[backcolor=transparent]([backcolor=transparent]"Signature"[backcolor=transparent]);
第二步:根据参数Key排序(顺序)
参考代码如下:
- [backcolor=transparent]java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]TreeMap[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]>[backcolor=transparent] sortParas [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]TreeMap[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]>();
- [backcolor=transparent]sortParas[backcolor=transparent].[backcolor=transparent]putAll[backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent]);
第三步:构造待签名的请求串
首先介绍下面会用到的特殊URL编码这个是POP特殊的一种规则,即在一般的URLEncode后再增加三种字符替换:加号(+)替换成 %20、星号(*)替换成 %2A、%7E 替换回波浪号(~)参考代码如下:
- [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] specialUrlEncode[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent] value[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]throws[backcolor=transparent] [backcolor=transparent]Exception[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]net[backcolor=transparent].[backcolor=transparent]URLEncoder[backcolor=transparent].[backcolor=transparent]encode[backcolor=transparent]([backcolor=transparent]value[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"UTF-8"[backcolor=transparent]).[backcolor=transparent]replace[backcolor=transparent]([backcolor=transparent]"+"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"%20"[backcolor=transparent]).[backcolor=transparent]replace[backcolor=transparent]([backcolor=transparent]"*"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"%2A"[backcolor=transparent]).[backcolor=transparent]replace[backcolor=transparent]([backcolor=transparent]"%7E"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"~"[backcolor=transparent]);
- [backcolor=transparent]}
构造待签名的请求串这里有两步动作第1步,把排序后的参数顺序拼接成如下格式:
- specialUrlEncode(参数Key) + “=” + specialUrlEncode(参数值)