HTTP协议及签名怎么实现?(2)-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

HTTP协议及签名怎么实现?(2)

2017-10-25 14:02:31 1280 0


2.3 生成签名


签名是为了让请求合法,共分为五步:

第一步:请求参数

  1. 请求参数包括系统参数和业务参数,不要遗漏
  2. 参数Key中不能包含最终要生成签名Key(也就是说Signature这个Key是关键字)

参考代码如下
  1. [backcolor=transparent]String[backcolor=transparent] accessKeyId [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"testId"[backcolor=transparent];
  2. [backcolor=transparent]String[backcolor=transparent] accessSecret [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"testSecret"[backcolor=transparent];
  3. [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]);
  4. [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时区
  5. [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]>();
  6. [backcolor=transparent]// 1. 系统参数
  7. [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignatureMethod"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"HMAC-SHA1"[backcolor=transparent]);
  8. [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]());
  9. [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"AccessKeyId"[backcolor=transparent],[backcolor=transparent] accessKeyId[backcolor=transparent]);
  10. [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignatureVersion"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"1.0"[backcolor=transparent]);
  11. [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]()));
  12. [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Format"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"XML"[backcolor=transparent]);
  13. [backcolor=transparent]// 2. 业务API参数
  14. [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Action"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"SendSms"[backcolor=transparent]);
  15. [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Version"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"2017-05-25"[backcolor=transparent]);
  16. [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"RegionId"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"cn-hangzhou"[backcolor=transparent]);
  17. [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"PhoneNumbers"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"15300000001"[backcolor=transparent]);
  18. [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignName"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"阿里云短信测试专用"[backcolor=transparent]);
  19. [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"TemplateParam"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"{\"customer\":\"test\"}"[backcolor=transparent]);
  20. [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"TemplateCode"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"SMS_71390007"[backcolor=transparent]);
  21. [backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"OutId"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"123"[backcolor=transparent]);
  22. [backcolor=transparent]// 3. 去除签名关键字Key
  23. [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]containsKey[backcolor=transparent]([backcolor=transparent]"Signature"[backcolor=transparent]))
  24. [backcolor=transparent]    paras[backcolor=transparent].[backcolor=transparent]remove[backcolor=transparent]([backcolor=transparent]"Signature"[backcolor=transparent]);


第二步:根据参数Key排序(顺序)


参考代码如下:
  1. [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]>();
  2. [backcolor=transparent]sortParas[backcolor=transparent].[backcolor=transparent]putAll[backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent]);


第三步:构造待签名的请求串


首先介绍下面会用到的特殊URL编码这个是POP特殊的一种规则,即在一般的URLEncode后再增加三种字符替换:加号(+)替换成 %20、星号(*)替换成 %2A、%7E 替换回波浪号(~)参考代码如下:
  1. [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]{
  2. [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]);
  3. [backcolor=transparent]}

构造待签名的请求串这里有两步动作第1步,把排序后的参数顺序拼接成如下格式:
  • specialUrlEncode(参数Key) + “=” + specialUrlEncode(参数值)
取消 提交回答
全部回答(0)
相关问答

0

回答

HTTP协议及签名怎么实现?(4)

2017-10-25 14:03:15 1240浏览量 回答数 0

0

回答

HTTP协议及签名怎么实现?(1)

2017-10-25 14:02:06 1375浏览量 回答数 0

1

回答

jsp中HTTP请求的方法主要有哪些?

2021-10-28 00:38:28 229浏览量 回答数 1

1

回答

使用Netty通信实现http协议请求支持,在运行过程中出现异常报错:对方连接重置异常,该如何解决?

2021-10-28 01:20:08 273浏览量 回答数 1

0

回答

Apache Http请求异常,求解??

2021-10-22 10:05:19 297浏览量 回答数 0

0

回答

http请求出现异常 求解?

2021-10-22 11:05:30 298浏览量 回答数 0

1

回答

http://www.msh91.com/ 首页打开有一个弹窗请求失败

2021-09-03 12:52:21 650浏览量 回答数 1

1

回答

数据源只能使用https请求吗 http不可以吗

2021-05-07 21:31:02 960浏览量 回答数 1

1

回答

使用HTTP请求存储在OSS的图片出现403错误

2021-04-30 17:02:39 584浏览量 回答数 1

1

回答

HTTP请求静态网页时返回412错误,什么原因?

2016-05-31 18:25:32 7008浏览量 回答数 1
+关注
350
文章
1300
问答
问答排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载