首页 > 解决方案 > 是否将其设为静态方法....这是个问题

问题描述

我遇到了模型脚本处理令牌验证的这段代码:

def encode_auth_token(self, user_id):
    try:
        payload = {
               'exp': datetime.datetime.utcnow() + datetime.timedelta(
                    days=current_app.config.get('TOKEN_EXPIRATION_DAYS'), \
                    seconds=current_app.config.get('TOKEN_EXPIRATION_SECONDS')),
               'iat': datetime.datetime.utcnow(),
               'sub': user_id
        }
       
       return jwt.encode(payload, current_app.config.get('SECRET_KEY'), algorithm='HS256')
   except Exception as e:
       return e

@staticmethod
def decode_auth_token(token):
   try:
       return jwt.decode(token, current_app.config.get('SECRET_KEY'))
   except jwt.ExpiredSignatureError:
       return 'Signature expired. Please log in again.'
   except jwt.InvalidTokenError:
       return 'Invalid token. Please log in again.'

我对这段代码的问题是为什么decode_auth_token方法需要是静态方法encode_auth_token而不需要?

标签: python

解决方案


推荐阅读