login_required装饰器不起作用,flask-Login允许匿名用户 [英] login_required decorator doesnt work, flask-Login permits anonymous users

查看:94
本文介绍了login_required装饰器不起作用,flask-Login允许匿名用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 login_required 装饰了一个方法,但令我惊讶的是它根本没有执行,允许匿名用户使用.在方法中打印 current_user 会返回以下内容:

I decorated a method with login_required, but I am surprised that its not executing at all, allowing in anonymous users. Printing the current_user within the method returns this:

<flask_login.AnonymousUserMixin object at 0xb67dbd4c>

是否不应该拒绝在 user.is_autheticated()中返回false的用户?我做错了什么?

Is it not supposed to reject users which return false in user.is_autheticated()? What did I do wrong?

我以这种方式设置了FL:

I have setup FL this way:

lm = LoginManager(app)
lm.login_view = 'root'

在views.py中:

in views.py:

@lm.user_loader
def load_user(id):
    return User.query.get(int(id))

实际视图:

@login_required
@app.route("/messages")
def messages():
    print "current user", current_user
    return "hello world"

推荐答案

机缘巧合给了我:

在应用其他装饰器时,请始终记住route()装饰器是最外面的:

When applying further decorators, always remember that the route() decorator is the outermost:

我写错了方法(不是最外层的路线).

I wrote it the wrong way (route not the outermost).

PDB 可以在调试模式下执行可疑方法,以检查本地状态.

PDB can execute your suspect method in debug mode, to inspect the local state.

Flask-Login仍然存在于GitHub中,并且 login_required 的来源非常简单易懂.

Flask-Login is present in GitHub anyway and the source of login_required is simple enough to understand.

这篇关于login_required装饰器不起作用,flask-Login允许匿名用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆