首页
仓库
文档
nginx手册
Docker手册
workerman
Flask
PHP
python
RabbitMQ
其他
Linux
占位1
占位2
目录
pip install pyjwt ```python import jwt import datetime #略 @index_index.route('/jwt_login') def jwt_login(): app = current_app._get_current_object() #导入app对象 payload = { "userid": 888, # 过期时间要用0时区的。 "exp": datetime.datetime.utcnow() + datetime.timedelta(days=1) } # 加密并获取token token = jwt.encode(payload, key=app.config['SECRET_KEY'], algorithm="HS256") print(token) #测试验证 #data=jwt.decode(token, app.config['SECRET_KEY'], algorithms=["HS256"]) #print(data) return "token: %s" % token @index_index.route('/jwt_check') def jwt_check(): print('开始校验token') app = current_app._get_current_object() print(request.headers.get("Authorization")) try: jwt_decode = jwt.decode(request.headers.get("Authorization"), key=app.config['SECRET_KEY'], algorithms=["HS256"]) print(jwt_decode) return jwt_decode except Exception as e: print(e) return "token校验出错" ``` ###官方DEMO ```python pip install pyjwt >>> import jwt >>> encoded_jwt = jwt.encode({"some": "payload"}, "secret", algorithm="HS256") >>> print(encoded_jwt) eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.4twFt5NiznN84AWoo1d7KO1T_yoc0Z6XOpOVswacPZg >>> jwt.decode(encoded_jwt, "secret", algorithms=["HS256"]) {'some': 'payload'} ```