【问题标题】:Hide Flask-Admin route隐藏 Flask-Admin 路由
【发布时间】:2017-03-24 12:31:52
【问题描述】:

我现在正在构建一个 Flask 博客并设置一个管理界面。我已阅读有关为 Flask-Admin 设置安全性的信息。我已经设法为我的所有模型设置了安全性(访问仅限于登录用户),但用户仍然可以访问“/admin”路由,其中​​只有一个简单的主页按钮。

我的问题是:有什么方法可以隐藏或保护“/admin”路由,因此未经身份验证的用户只会被重定向到登录页面/拒绝访问?

非常感谢!

附加我当前的管理员设置:

from flask_admin import Admin
from flask_login import current_user
from flask_admin.contrib import sqla
from wtforms.widgets import TextArea
from wtforms import TextAreaField
from samo.models import User, Post, Tag
from samo import app,db

admin = Admin(app, name='Admin', template_mode='bootstrap3')

class CKTextAreaWidget(TextArea):
    def __call__(self, field, **kwargs):
        if kwargs.get('class'):
            kwargs['class'] += ' ckeditor'
        else:
            kwargs.setdefault('class', 'ckeditor')
        return super(CKTextAreaWidget, self).__call__(field, **kwargs)      

class CKTextAreaField(TextAreaField):
    widget = CKTextAreaWidget()

class PostAdmin(sqla.ModelView):
    form_overrides = dict(content=CKTextAreaField)
    create_template = 'blog/ckeditor.html'
    edit_template = 'blog/ckeditor.html'
    form_excluded_columns = ('slug')    
    def is_accessible(self):
        return current_user.is_authenticated
admin.add_view(PostAdmin(Post, db.session))

class TagAdmin(sqla.ModelView):
    def is_accessible(self):
        return current_user.is_authenticated
admin.add_view(TagAdmin(Tag, db.session))

class UserAdmin(sqla.ModelView):
    def is_accessible(self):
        return current_user.is_authenticated
admin.add_view(UserAdmin(User, db.session))

【问题讨论】:

  • 如果你隐藏了一个路由,你将如何访问它?就像你说的那样,如果没有经过身份验证,为什么不能使用重定向?

标签: python python-3.x flask flask-admin flask-security


【解决方案1】:

我为我的所有网站都使用了您所描述的这种配置。使用AdminIndexView。这是一个在用户未授权的情况下如何处理登录、注销和重定向的示例。

class FlaskyAdminIndexView(AdminIndexView):

    @expose('/')
    def index(self):
        if not login.current_user.is_authenticated:
            return redirect(url_for('.login'))
        return super(FlaskyAdminIndexView, self).index()

    @expose('/login', methods=['GET', 'POST'])
    def login(self):
        form = LoginForm(request.form)
        if helpers.validate_form_on_submit(form):
            user = form.get_user()
            if user is not None and user.verify_password(form.password.data):
                login.login_user(user)
            else:
                flash('Invalid username or password.')
        if login.current_user.is_authenticated:
            return redirect(url_for('.index'))
        self._template_args['form'] = form
        return super(FlaskyAdminIndexView, self).index()

    @expose('/logout')
    @login_required
    def logout(self):
        login.logout_user()
        return redirect(url_for('.login'))

在您创建管理对象的__init__.py 中执行以下操作:

admin = Admin(index_view=FlaskyAdminIndexView())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2017-01-31
    • 2016-09-28
    • 1970-01-01
    • 2015-07-18
    • 2011-06-07
    • 2018-07-24
    相关资源
    最近更新 更多