我创建了 FastApi 应用程序。sign_jwt 函数返回令牌:
def sign_jwt(email: str) -> str:
payload = {
'email': email,
'expires': (datetime.utcnow() + timedelta(days=14)).strftime('%d/%m/%Y %H:%M')
}
token = jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
return token
它返回有效的令牌,就像这样:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6ImtldmluQG1haWwucnUiLCJleHBpcmVzIjoiMTIvMTEvMjAyMSAwNDoxNSJ9.vwOg7Vewx06rQ3LwXA_rBOWzyc7FaN5Kxk0fqqDwMwo
但是当这个函数在另一个内部工作时:
@app.post('/auth')
async def login_user(user: UserLogin) -> Union[dict, JSONResponse]:
response = await check_user(user)
if 'email' in response.keys():
return {'token': sign_jwt(response['email'])}
return JSONResponse(status_code=402,
content=response)
它返回没有签名的令牌,如下所示:
eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJlbWFpbCI6ImtldmluQG1haWwucnUiLCJleHBpcmVzIjoiMTIvMTEvMjAyMSAwNDoyNiJ9.
不知道发生了什么。