我正在尝试使用Flask-KVSession作为 Flask 网站的替代会话实现。我创建了一个测试网站(参见下面的代码 1)。当我运行它时,我可以使用浏览器通过在我的网络浏览器中的各种资源之间导航来将值存储到会话中。这可以正常工作。此外,当我查看sessions
生成的 SQLite 数据库中的表时,我看到一条记录一直用于存储该会话。
然后我尝试将Flask-Security添加到此(参见下面的代码 2)。运行此站点后(确保首先删除现有的 test.db sqlite 文件),我被带到登录提示并登录。然后我继续做同样的事情,在资源之间来回跳转。我得到相同的结果。
问题是当我查看 sqlitebrowsersessions
表时,有 8 条记录。事实证明,为每个发出的请求创建了一个新的会话记录。
为什么在使用 Flask-Security 时会为每个请求创建一个新的会话记录?为什么现有会话没有像以前那样更新?
代码 1(没有 Flask-Security 的 KVSession)
import os
from flask import Flask, session
app = Flask(__name__)
app.secret_key = os.urandom(64)
#############
# SQLAlchemy
#############
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
DB_DIR = os.path.dirname(os.path.abspath(__file__))
DB_URI = 'sqlite:////{0}/test.db'.format(DB_DIR)
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI
@app.before_first_request
def create_user():
db.create_all()
############
# KVSession
############
from simplekv.db.sql import SQLAlchemyStore
from flask.ext.kvsession import KVSessionExtension
store = SQLAlchemyStore(db.engine, db.metadata, 'sessions')
kvsession = KVSessionExtension(store, app)
@app.route('/a')
def a():
session['last'] = 'b'
return 'Thank you for visiting A!'
@app.route('/b')
def b():
session['last'] = 'b'
return 'Thank you for visiting B!'
@app.route('/c')
def c():
return 'You last visited "{0}"'.format(session['last'])
app.run(debug=True)
代码 2(带有 Flask-Security 的 KVSession)
import os
from flask import Flask, session
app = Flask(__name__)
app.secret_key = os.urandom(64)
#############
# SQLAlchemy
#############
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
DB_DIR = os.path.dirname(os.path.abspath(__file__))
DB_URI = 'sqlite:////{0}/test.db'.format(DB_DIR)
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI
###########
# Security
###########
# This import needs to happen after SQLAlchemy db is created above
from flask.ext.security import (
Security, SQLAlchemyUserDatastore, current_user,
UserMixin, RoleMixin, login_required
)
# Define models
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'))
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
@app.before_first_request
def create_user():
db.create_all()
user_datastore.create_user(email='test@example.com', password='password')
db.session.commit()
############
# KVSession
############
from simplekv.db.sql import SQLAlchemyStore
from flask.ext.kvsession import KVSessionExtension
store = SQLAlchemyStore(db.engine, db.metadata, 'sessions')
kvsession = KVSessionExtension(store, app)
@app.route('/a')
@login_required
def a():
session['last'] = 'b'
return 'Thank you for visiting A!'
@app.route('/b')
@login_required
def b():
session['last'] = 'b'
return 'Thank you for visiting B!'
@app.route('/c')
@login_required
def c():
return 'You last visited "{0}"'.format(session['last'])
app.run(debug=True)
版本信息
Python 2.7.3
Flask==0.9
Flask==0.9
Flask-KVSession==0.3.2
Flask-Login==0.1.3
Flask-Mail==0.8.2
Flask-Principal==0.3.5
Flask-SQLAlchemy==0.16
Flask-Security==1.6.3
SQLAlchemy==0.8.1