flask-login 生成 cookie,session
Flask-Login login_user() 显示来自 Set-Cookie 标头的加密 cookie
# 模拟一个用户类
class User(UserMixin):
def __init__(self, id):
self.id = id
@app.route('/login')
def login():
# 模拟用户登录过程
user = User('1')
login_user(user)
from flask.sessions import SecureCookieSessionInterface
from flask import session
sessions_data = dict(session)
cookie_value = SecureCookieSessionInterface().get_signing_serializer(app).dumps(
{
'_fresh': sessions_data.get("_fresh"),
'_id': sessions_data.get("_id"),
'_user_id': sessions_data.get("_user_id")
}
)
print(cookie_value)
return 'You have been logged in\n' + cookie_value