当前位置: 首页 > 面试题库 >

Flask应用程序中@login_required问题

端木兴国
2023-03-14
问题内容

我已经创建了一个处理身份验证的蓝图。此蓝图使用Flask-Login。并具有以下内容以及未显示的更多代码。

在蓝图中,我具有以下内容:

from flask.ext.login import LoginManager
from flask.ext.login import UserMixin
from flask.ext.login import current_user
from flask.ext.login import login_required
from flask.ext.login import login_user
from flask.ext.login import logout_user

auth_print = Blueprint('auth_print', __name__)
login_manager = LoginManager()
login_manager.login_view = '/login'

class User(UserMixin):

  user_store = {}  # Stores the users that are already logged in.

  def __init__(self, user_id):
    self.user_store[user_id] = self  # add the user to the user_store
    self.username = user_id  # the user_id is in fact the username
    self.id = unicode(user_id)

  def sign_out(self):
    logout_user()
    try:
      del self.user_store[self.id]
    except KeyError:
      pass

  @classmethod
  def get(cls, user_id):
    return cls.user_store.get(user_id)

@login_manager.user_loader
def load_user(user_id):
  return User.get(user_id)

def get_current_user():
  return current_user

@login_required
@auth_print.route('/')
def user():
  return "Welcome, and thanks for logging in."

然后,我有一个小html" target="_blank">应用程序想要添加身份验证。

小型应用

import the_above_module
app.register_blueprint(the_above_module.auth_print) # register the blueprint

@the_above_module.login_required
@app.route('/profile')
def protected():
    name = the_above_module.get_current_user().username
    return "Thank you for logging in."

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

现在我知道该蓝图的@login_required可以正常工作,因为如果打开浏览器并转到localhost:8000 /,我必须登录。

但是,如果我转到localhost:8000 / profile,则login_required装饰器将永远不会触发。由于没有当前用户,因此出现错误。

为什么即使我确定要保持相同的名称空间,@ login_required也可以在蓝图中而不在应用程序中工作?


问题答案:

你必须更改装饰器的顺序。引用Flask文档:

那么,你现在将如何使用该装饰器?将其作为最里面的装饰器应用到视图函数。当应用其他装饰器时,请始终记住route()装饰器是最外面的:

@app.route('/secret_page') 
@login_required 
def secret_page():
    pass


 类似资料:
  • 主要内容:调试模式要测试Flask安装是否成功,在编辑器中输入以下代码,并保存到文件: 中。 在项目中导入模块是强制性的。 Flask类的一个对象是WSGI应用程序。 Flask构造函数将当前模块的名称()作为参数。 Flask类的函数是一个装饰器,它告诉应用程序哪个URL应该调用相关的函数。 rule 参数表示与该函数绑定的URL。 options 是要转发给底层Rule对象的参数列表。 在上面的例子中, URL

  • 问题内容: 我有一个现有的Flask应用程序,并且想找到通往另一个应用程序的路线。更具体地说,第二个应用程序是Plotly Dash应用程序。如何在现有的Flask应用程序中运行Dash应用程序? 我还尝试将路由添加到Dash实例,因为它是Flask应用程序,但出现错误: 问题答案: 从文档: 基本的Flask应用程序可从访问app.server。 你还可以将自己的Flask应用实例传递到Dash

  • 我试图把我的头缠在这个问题上2天现在,我找不到任何正确的解决方案在网上和论坛上这个问题。 我无法使用@login\u required decorator在flask应用程序中保护dash应用程序。我可以使用blueprint route中的@login\u调用dash应用程序,以确保其安全并将其重定向到dash应用程序。 但是如果我直接调用dash-applike_localhost:5000/

  • 问题内容: 我正在使用应用程序工厂模式来设置我的Flask应用程序。我的应用程序使用Flask- Babel扩展程序,该扩展程序也在工厂中设置。但是,我想以蓝图的形式访问该扩展程序以便使用它, 工厂在。 我想添加以下内容: 不幸的是,无法从应用程序工厂访问变量。我应该如何解决这个问题? 问题答案: 正是这种情况下,Flask扩展被设计为无需应用程序实例即可实例化。在工厂外,定义扩展。在工厂内部,致

  • 问题内容: 我想知道启动Flask应用程序的正确方法。该文档显示了两个不同的命令: 和 产生相同的结果并正确运行该应用程序。 两者之间有什么区别,以及用于运行Flask应用程序的区别是什么? 问题答案: 该命令是用于与Flask应用进行交互的CLI。该文档介绍了如何使用CLI命令和添加自定义命令。该命令是启动开发服务器的首选方法。 使用环境变量将命令指向您的应用程序。设置为与调试器和重新加载器一起

  • 问题内容: 我有一个使用Flask用Python编写的小应用程序。现在,我正在nohup下运行它,但是我想使其守护进程。这样做的正确方法是什么? 问题答案: 使用gunicorn部署可能是最简单的方法,请先安装gunicorn,然后: 尽管你可能想使用主管或类似性质的工具来监视黑胶皮(at the very least use – so you can reload/stop gunicorn e