最好的方法,使Flask登录的login_required默认 [英] Best way to make Flask-Login's login_required the default

查看:1254
本文介绍了最好的方法,使Flask登录的login_required默认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像这样的问题:使Django的login_required默认



我正在使用 Flask-Login login_required 装饰者。有没有办法使它成为 Flask 中的默认行为?

解决方案

我在乐器项目。

 b def check_valid_login():
login_valid =会话中的'user'或用来检查有效登录的任何内容

if(request.endpoint和$ b $'static'not in request .endpoint和
不是login_valid和
不是getattr(app.view_functions [request.endpoint],'is_public',False)):
return render_template('login.html',next = request .endpoint)

然后我创建了一个 is_public()
$ $ $ $ c $ def def public_endpoint(function)$ b $ $ c $> $ c $> $ decorator对于需要在不登录的情况下可以访问的几个地方:

b function.is_public = True
返回函数


Like this question: Best way to make Django's login_required the default

I'm using Flask-Login's login_required decorator now. Is there anyway to make it the default behavior in Flask?

解决方案

I did this in my instruments project. I use the before_request decorator:

@app.before_request
def check_valid_login():
    login_valid = 'user' in session # or whatever you use to check valid login

    if (request.endpoint and 
        'static' not in request.endpoint and 
        not login_valid and 
        not getattr(app.view_functions[request.endpoint], 'is_public', False) ) :
        return render_template('login.html', next=request.endpoint)

and I then created an is_public() decorator for the few places that would need to be accessible without login:

def public_endpoint(function):
    function.is_public = True
    return function

这篇关于最好的方法,使Flask登录的login_required默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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