Flask Basic HTTP Auth使用登录页面 [英] Flask Basic HTTP Auth use login page

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

问题描述

我正在构建一个测试应用程序,并使用这里的说明( http://flask.pocoo。 org / snippets / 8 / )来设置简单的身份验证。在需要身份验证的页面上,出现一个弹出窗口,提示需要授权。
而不是那个,我想重定向到一个登录页面,用户可以把他们的用户/通过在一个窗体。



这是我有目前(与链接中的代码片段相同):

  from functools import flask 
from flask import request,Response

$ b $ def check_auth(用户名,密码):
调用此函数检查用户名/
密码组合是否有效

返回用户名=='管理员'和密码=='秘密'

def authenticate():
发送401响应,启用基本身份验证
返回响应(
'无法验证您的访问级别为该URL.\\\
'
'您必须以正确的凭据登录',401,
{'WWW (a):验证':'Basic realm =需要登录'))

def require_auth(f):
@wraps(f)
def装饰(* args,** kwargs):
auth = request.authorization
if不是auth或不是check_auth(auth.username,auth.password):
返回authenticate()
返回f(* args,** kwargs)
返回装饰

看起来我可以使用Flask-Auth,但是我真的只需要上面提供的功能。
$ b

感谢,
Ryan

解决方案

=http://flask.pocoo.org/docs/0.10/patterns/viewdecorators/ =nofollow> http://flask.pocoo.org/docs/0.10/patterns/viewdecorators/ 。有一个示例装饰器可以做到这一点:

 来自functools import wrapps $ b $ from flask import g,request,redirect ,url_for 
$ b def login_required(f):
@wraps(f)
def decorated_function(* args,** kwargs):
如果g.user是None :
return redirect(url_for('login',next = request.url))
return f(* args,** kwargs)
return decorated_function


$ b


I'm building a test app and used the instructions here (http://flask.pocoo.org/snippets/8/) to setup simple authentication. On pages where auth is required I get a popup saying "Authorization Required". Instead of that, I'd like to redirect to a login page where the user can put their user/pass in a form.

Here's what I have currently (same as the snippet in the link):

from functools import wraps
from flask import request, Response


def check_auth(username, password):
    """This function is called to check if a username /
    password combination is valid.
    """
    return username == 'admin' and password == 'secret'

def authenticate():
    """Sends a 401 response that enables basic auth"""
    return Response(
    'Could not verify your access level for that URL.\n'
    'You have to login with proper credentials', 401,
    {'WWW-Authenticate': 'Basic realm="Login Required"'})

def requires_auth(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization
        if not auth or not check_auth(auth.username, auth.password):
            return authenticate()
        return f(*args, **kwargs)
    return decorated

It looks like I could use Flask-Auth, but I really only need the functionality that the above provides.

Thanks, Ryan

解决方案

From the documentation, here: http://flask.pocoo.org/docs/0.10/patterns/viewdecorators/. There is a sample decorator that does just this:

from functools import wraps
from flask import g, request, redirect, url_for

def login_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if g.user is None:
            return redirect(url_for('login', next=request.url))
        return f(*args, **kwargs)
    return decorated_function

Just return the redirect instead of calling the authenticate() method as you do now.

这篇关于Flask Basic HTTP Auth使用登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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