python 登陆redirect重定向https_python – Flask蓝图和登录重定向问题

我有一个

sample Flask app,主要是工作.基本URL(/)不需要身份验证;但是,我有/ auth / secured资源,需要身份验证.

只要用户登录,该应用程序就可以正常运行;然而,它抛出了一个werkzeug.routing.BuildError(BuildError:无法使用值[‘next’]为端点’login’构建url.当你有人试图访问/ auth / secured时,你的意思是’auth.login’吗?)作为一个未经身份验证的用户,因为我有这样编码:

@auth.route('/secured')

@ldap.login_required

def secured():

return 'Login Success! {0}'.format(session)

我正在使用Flask Blueprints进行URL路由…我认为这应该有效……

def create_app(config_name):

from .main import main as main_blueprint

from .auth import auth as auth_blueprint

# Configure the flask instance...

app = Flask(__name__)

app.config.from_object(config[config_name])

config[config_name].init_app(app)

# Initialize the application...

bootstrap.init_app(app)

db.init_app(app)

ldap.init_app(app)

# Blueprint registration

app.register_blueprint(main_blueprint)

app.register_blueprint(auth_blueprint, url_prefix='/auth')

return app

app目录的构建如下:

testapp/

|

+ config.py

+ manage.py

+ app/

|

+ __init__.py

+ auth/

|

+ __init__.py

+ views.py

+ main/

|

+ __init__.py

+ views.py

我可以说,在我的蓝图注册中,有些东西搞砸了;但是,我对错误所在的地方感到茫然.

如何将未经身份验证的用户作为未经身份验证的用户进行/ auth / secure冲浪时,将其正确重定向到/ auth / login URL?

最佳答案 看起来有几个问题.

第一个是缺少配置参数LDAP_LOGIN_VIEW. The default is login但由于您没有名为“login”的视图,您可能希望在配置文件中将其设置为auth.login:

LDAP_LOGIN_VIEW = 'auth.login'

下一个问题是你的auth.login不处理下一个参数.下一个参数告诉登录功能在成功登录后重定向的位置.这可能会导致问题,因为flask-simpleldap是passing a next parameter here.

return redirect(url_for(current_app.config['LDAP_LOGIN_VIEW'],

next=request.path))

例:

from flask import request

@app.route('/login', methods=['POST', 'GET'])

def login():

error = None

if request.method == 'POST':

if valid_login(request.form['username'],

request.form['password']):

return log_the_user_in(request.form['username'])

else:

error = 'Invalid username/password'

# the code below is executed if the request method

# was GET or the credentials were invalid

return render_template('login.html', error=error)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值