鍍金池/ 問(wèn)答/Python  網(wǎng)絡(luò)安全/ flask-login中的Request Loader是如何定制登錄的?

flask-login中的Request Loader是如何定制登錄的?

1.在flask中使用了flask-login來(lái)做用戶登錄,但是如何讓flask-login支持同時(shí)支持token和 Authorization header登錄呢?
看官方文檔上寫的是支持token和Authorization header的,但是具體怎么用寫的不是很楚,能否同時(shí)支持三種驗(yàn)證方式呢?

@login_manager.request_loader
def load_user_from_request(request):

    # first, try to login using the api_key url arg
    api_key = request.args.get('api_key')
    if api_key:
        user = User.query.filter_by(api_key=api_key).first()
        if user:
            return user

    # next, try to login using Basic Auth
    api_key = request.headers.get('Authorization')
    if api_key:
        api_key = api_key.replace('Basic ', '', 1)
        try:
            api_key = base64.b64decode(api_key)
        except TypeError:
            pass
        user = User.query.filter_by(api_key=api_key).first()
        if user:
            return user

    # finally, return None if both methods did not login the user
    return None
回答
編輯回答
編輯回答
氕氘氚

使用flask-httpauth插件,里面自帶token驗(yàn)證

2017年9月29日 14:50