Fastapi token验证
服务端:
/security.py:
import hashlib
import hmac
from fastapi import HTTPException, Header
import time
SECRET = '123' # 秘钥串,自定义
def get_sign(username: str, nonce: str, ts: str, sk: str) -> str:
"""
生成签名
ak:也可以使用各自的id
nonce:随机值
ts:10位时间戳
sk:secret加密用
"""
a = [username, nonce, ts, sk]
a.sort()
# a = [self.ak, 'ZPMxNpPhmrPzQj27AGKijM3FmEcHW4BY', '1550032562']
join_str = "".join(a)
return hmac.new(sk.encode(), join_str.encode(), hashlib.sha256).hexdigest