当我使用@roles_required装饰器并且没有必需的角色时,不会被重定向到设置页面。实际上,我根本没有被重定向,但是当我手动注销并重定向到/ login时,它会显示我应该收到的所有错误消息。
这就是我要做的事情:
两条消息中的第一条可能是对我尝试进入/ authtest的回应,第二条消息是因为/ logout将我重定向到首页,由于/ login_required装饰器,该页面又将我重定向到/ login。 我不明白的是为什么主页上的第4步没有显示“没有权限”消息。
该代码主要来自flask-security快速入门示例:
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_security import Security, SQLAlchemyUserDatastore, \
UserMixin, RoleMixin, login_required, roles_required
# Create app
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret'
app.config['SECURITY_PASSWORD_SALT'] = 'super-secret-salt'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///mdb.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECURITY_UNAUTHORIZED_VIEW'] = '/redir'
# Create database connection object
db = SQLAlchemy(app)
# 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'))
# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
# Create some testusers
@app.before_first_request
def create_user():
db.create_all()
# Create the Roles "admin" and "end-user" -- unless they already exist
user_datastore.find_or_create_role(name='admin', description='Administrator')
user_datastore.find_or_create_role(name='end-user', description='End user')
# Create some default users if they don't exist already
if not user_datastore.get_user('admin@dummy.net'):
user_datastore.create_user(email='admin@dummy.net', password='pass')
if not user_datastore.get_user('user@dummy.net'):
user_datastore.create_user(email='user@dummy.net', password='pass')
# Commit any database changes; the User and Roles must exist before we can add a Role to the User
db.session.commit()
# Assign the admin role to the created user
user_datastore.add_role_to_user('admin@dummy.net', 'admin')
user_datastore.add_role_to_user('user@dummy.net', 'end-user')
db.session.commit()
# Views
@app.route('/')
@login_required
def home():
return render_template('index.html')
@app.route('/authtest')
@roles_required('admin')
def authtest():
return render_template('authtest.html')
@app.route('/redir')
def redir():
return render_template('redir.html')
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)
答案 0 :(得分:1)
这是flask-security中的错误,请参阅: https://github.com/mattupstate/flask-security/issues/677 修复此PR从未合并: https://github.com/mattupstate/flask-security/pull/726
此问题已在我维护的fork中修复: https://pypi.org/project/Flask-Security-Too/
我唯一可以提供的解决方法是定义您自己的security.unauthorized_callback并在那里执行重定向。