目录:导读
前言
一般公司对外的接口都会用到 sign 签名,对不同的客户提供不同的apikey ,这样可以提高接口请求的安全性,避免被人抓包后修改请求参数乱请求。
接口sign签名
一登陆的接口请求为例,如下接口抓包报文信息,其中sign的签名规则如下
第一步,拼接字符串,首先去除sign参数本身,然后去除值是空的参数p3,剩下p2=v2&p1=v1&method=cancel&pn=vn,
然后按参数名字符升序排序,method=cancel&p1=v1&p2=v2&pn=vn.
第二步,然后做参数名和值的拼接,最后得到methodcancelp1v1p2v2pnvn
第三步,在上面拼接得到的字符串后加上验证密钥apikey,我们假设是abc,得到新的字符串methodcancelp1v1p2v2pnvnabc
第四步,然后将这个字符串换为小写进行md5计算,假设得到的是abcdef,这个值即为sign签名值。
注意:计算md5之前请确保接口与接入方的字符串编码一致,如统一使用utf-8编码或者GBK编码,如果编码方式不一致则计算出来的签名会校验失败。
POST http://127.0.0.1:8000/api/v3/login HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Host: 127.0.0.1:8000
Content-Length: 111
{
"username": "test",
"password": "123456",
"sign": "1aca01806e93bb408041965a817666af"
}
HTTP/1.1 200 OK
Date: Sat, 26 Oct 2019 03:38:31 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Content-Type: application/json
Vary: Accept, Cookie
Allow: POST, OPTIONS
X-Frame-Options: SAMEORIGIN
Content-Length: 109
{
"code": 0, "msg": "login success!", "username": "test", "token": "a76ba3b8fcbdff82f6a94e5ad5bf8fb934192e5f"}
实现 sign 签名
在 conftest.py 编写 pre_sign 函数,对请求的 body 部分预处理
import hashlib
from pytest_yaml_yoyo import my_builtins
def sign_body(body: dict) -> str:
"""对body 签名"""
key = "12345678" # 接口项目的开发提供
# 去掉sign 和值为空的
new_body = [''.join(item) for item in body.items() if item[0] != 'sign' and item[1] != '']
# print(new_body)
# 做排序
new_body.sort()
# 拼接
# print(new_body)
str_body = ''.join(new_body) + key
# print(str_body)
# md5加密
def jiamimd5(src: str):
"""md5加密"""
m = hashlib.md5()
m.update(src.encode('UTF-8'))
return m.hexdigest()
sign = jiamimd5(str_body)
return sign
def pre_sign(req: dict):
print(f'请求预处理:{
req}')
sign = sign_body(req.get('json')