parent
7875121b51
commit
4a3f7bf5d0
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,7 +0,0 @@
|
||||
import jwt
|
||||
|
||||
|
||||
def decode_jwt_token(encoded_jwt):
|
||||
# 关闭过期时间检验
|
||||
de_code = jwt.decode(encoded_jwt, "JWT_SECRET_KEY", algorithms=['HS256'])
|
||||
return de_code
|
@ -1,14 +0,0 @@
|
||||
def get_jwt_token(user_name, role_data='default'):
|
||||
"""
|
||||
生成jwt-token
|
||||
:param unit_name:
|
||||
:param role_data:
|
||||
:return:
|
||||
"""
|
||||
payload = {
|
||||
'exp': datetime.utcnow() + timedelta(seconds=3600), # 单位秒
|
||||
'iat': datetime.utcnow(),
|
||||
'data': {'username': user_name, 'role_data': role_data}
|
||||
}
|
||||
encoded_jwt = jwt.encode(payload, "JWT_SECRET_KEY", algorithm='HS256')
|
||||
return str(encoded_jwt, encoding='utf8')
|
@ -0,0 +1,33 @@
|
||||
from datetime import datetime, timedelta
|
||||
from serve.settings import JWT_SECRET_KEY, JWT_ALGORITHM
|
||||
from jwt import ExpiredSignatureError
|
||||
import jwt
|
||||
|
||||
|
||||
def get_jwt_token(user_name, role_data='default'):
|
||||
"""
|
||||
生成jwt-token
|
||||
:param unit_name:
|
||||
:param role_data:
|
||||
:return:
|
||||
"""
|
||||
payload = {
|
||||
'exp': datetime.utcnow() + timedelta(seconds=10), # 单位秒
|
||||
'iat': datetime.utcnow(),
|
||||
'data': {'username': user_name, 'role_data': role_data}
|
||||
}
|
||||
encoded_jwt = jwt.encode(payload, JWT_SECRET_KEY, algorithm=JWT_ALGORITHM)
|
||||
return str(encoded_jwt)
|
||||
|
||||
|
||||
def decode_jwt_token(encoded_jwt):
|
||||
try:
|
||||
# 关闭过期时间检验
|
||||
decoded_token = jwt.decode(encoded_jwt, JWT_SECRET_KEY, algorithms=['HS256'])
|
||||
return decoded_token
|
||||
except ExpiredSignatureError:
|
||||
# JWT 令牌已过期
|
||||
return {'error': 'JWT token has expired'}
|
||||
except jwt.InvalidTokenError:
|
||||
# 其他 JWT 令牌验证错误
|
||||
return {'error': 'Invalid JWT token'}
|
Binary file not shown.
Loading…
Reference in new issue