当前位置:网站首页>api平台通用签名机制
api平台通用签名机制
2022-06-24 07:03:00 【一个不靠谱的程序员】
通用API平台需要对每一个访问请求进行身份验证。
借鉴阿里云的SDK。
用户机制
API平台里的每一个用户有自己的uid。
每一个用户可以有一个或者多个appKey和appSecret。即一组appKey和appSecret标记一个应用。
其中appKey用来管理权限。【唯一】(24位随机字符串)(lumen里可以使用str_random()生成)
appSecret用来签名。(32位随机字符串)(lumen里可以使用str_random()生成)
appKey为每次请求必传的参数
签名规则
排序请求字符串
对所有的请求参数,按照参数名称的字典顺序进行排序参数编码
对所有的请求参数的键值相连接然后进行RFC3986规则进行编码
编码规则为:
对于字符 A-Z、a-z、0-9 以及字符 - 、 _ 、 . 、 ~ 不编码。
对于其他字符编码成 %XY 的格式,其中 XY 是字符对应 ASCII 码的 16 进制表示。比如半角的双引号 ” 对应的编码就是 %22 。
对于扩展的 UTF-8 字符,编码成 %XY%ZA… 的格式。
需要说明的是半角的空格要被编码是 %20 ,而不是加号 + 。
注意:一般支持 URL 编码的库(比如 PHP 中的 urlencode )都是按照 application/x-www-form-urlencoded 的 MIME 类型的规则进行编码。可以直接使用这类方式进行编码,把编码后的字符串中加号 + 替换成 %20 、星号 * 替换成 %2A 、 %7E 替换回波浪号 ~ ,即可得到上述规则描述的编码字符串。构造用于计算签名的字符串
StringToSign=
//get或者post
HTTPMethod +
编码后的参数
- 计算HMAC值
按照 RFC2104 的定义,使用步骤 3 得到的字符串 StringToSign 计算签名 HMAC 值。
注意:计算签名时,使用的 Key 就是您的 appSecret,使用的哈希算法是 SHA1。 - 计算签名
按照 Base64 编码规则,把步骤 3 得到的 HMAC 值编码成字符串,即得到签名值(Signature)。 - 将签名作为signature参数添加到请求中
签名代码 PHP版本
//1.排序参数
ksort($parameters);
//2.对参数进行URL编码
$paramsStr = '';
foreach ($parameters as $k => $v) {
$paramsStr .= $k . $v;
}
$res = urlencode($paramsStr);
$res = preg_replace('/\+/', '%20', $res);
$res = preg_replace('/\*/', '%2A', $res);
$res = preg_replace('/%7E/', '~', $res);
//3.构造计算签名的字符串
$stringToSign = $request->getMethod() . $res;
//4.计算HMAC值
$hmacStr = hash_hmac('sha1', $stringToSign, $appSecret, true);
//5.返回签名值
$signature = base64_encode($hmacStr);
边栏推荐
- 2021-06-25: a batch of strings consisting only of lowercase letters (a~z) are put
- 12-- merge two ordered linked lists
- 贷款五级分类
- [micro services ~nacos] Nacos service providers and service consumers
- Promise的使用場景
- (PKCS1) RSA 公私钥 pem 文件解析
- Permission model DAC ACL RBAC ABAC
- os. path. Pits encountered during the use of join()
- leetcode 1642. Furthest Building You Can Reach(能到达的最远的建筑)
- Rescue system -- the application of read-write separation
猜你喜欢

日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用

2021-03-04 comp9021 class 6 notes

jwt(json web token)

ZUCC_ Principles of compiling language and compilation_ Experiment 01 language analysis and introduction
![[micro services ~nacos] Nacos service providers and service consumers](/img/b7/47ecd6979ccfeb270261681d6130be.png)
[micro services ~nacos] Nacos service providers and service consumers

小黑ai4code代码baseline啃食1

Small sample fault diagnosis - attention mechanism code - Implementation of bigru code parsing
![[explain the difference between operation and maintenance and network engineering]](/img/2b/945f468588e729336e2e973e777623.jpg)
[explain the difference between operation and maintenance and network engineering]

ZUCC_编译语言原理与编译_实验01 语言分析与简介

2022 tea artist (intermediate) work license question bank and online simulation examination
随机推荐
新准则金融资产三分类:AMC、FVOCI和FVTPL
Detailed explanation of etcd backup and recovery principle and actual record of stepping on the pit
[graduation season] Hello stranger, this is a pink letter
Several ways you can't move zero (sequel)
JUC个人简单笔记
05 Ubuntu installing mysql8
[acnoi2022] I have done it, but I can't
问题4 — DatePicker日期选择器,2个日期选择器(开始、结束日期)的禁用
2021-03-09 comp9021 class 7 Notes
Win10 cloud, add Vietnamese
Opencv实现图像的基本变换
OpenCV to realize the basic transformation of image
AUTO PWN
[real estate opening online house selection, WiFi coverage temporary network] 500 people are connected to WiFi at the same time
uniapp 热更新后台管理
Paper notes: multi label learning dm2l
数据库,查询本月借出书的数量,如果高于10本时,显示“本月借出书大于10本”,否则显示“本月借出书小于10本”
DHCP, TFTP Foundation
ZUCC_编译语言原理与编译_实验08 语法分析 LR 分析
饼状统计图,带有标注线,都可以自行设定其多种参数选项