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

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

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

@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驗證

2017年9月29日 14:50