我正在做一个项目,我的每个 REST
端点需要进行身份验证。一个例子是
@login_required
def get_transactions(self):
pass
我有一个
User
看起来像的模型class User(UserMixin, db.Model):
__tablename__ = 'users'
# noinspection PyShadowingBuiltins
uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True,
unique=True)
email = Column('email', String, nullable=False, unique=True)
_password = Column('password', String, nullable=False)
created_on = Column('created_on', sa.types.DateTime(timezone=True),
default=datetime.utcnow(), nullable=False)
last_login = Column('last_login', sa.types.DateTime(timezone=True),
onupdate=datetime.utcnow())
def __init__(self, email, password):
self.email = email
self._password = hash_password(password) # hash_password does md5 of password
我不知道它是如何工作的,请帮助
最佳答案
你见过这个例子吗? http://flask.pocoo.org/docs/patterns/viewdecorators/?highlight=login_required
如果您不想自己重写这些东西,也可以使用这些模块:
无论如何,至于在 session 中存储数据,这应该是相当容易的。简单地把它放在flask.session中:
import flask
# You want to automatically generate the token and store it somewhere on the server (database for example)
flask.session['some_token'] = some_token
您的 token 可能是这样的:
class Session(db.Model):
token = Column('token') # auto generated thingy
# foreign key to user for example
# expiration time/date
关于python - Flask:如何在 session 中存储凭据并检索它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15993408/