[英]Issue with Flask Blueprints and login redirects
I have a sample Flask app which mostly works. 我有一个样本Flask应用程序 ,主要工作。 The base URL (
/
) doesn't require authentication; 基本URL(
/
)不需要身份验证; however, I have a resource at /auth/secured
, which requires authentication. 但是,我有
/auth/secured
资源,需要身份验证。
The app works very well as long as a user is logged in; 只要用户登录,该应用程序就可以正常运行; however, it throws a
werkzeug.routing.BuildError (BuildError: Could not build url for endpoint 'login' with values ['next']. Did you mean 'auth.login' instead?)
when someone tries to access /auth/secured
as an unauthenticated user, because I have it coded like this: 然而,它抛出了一个
werkzeug.routing.BuildError (BuildError: Could not build url for endpoint 'login' with values ['next']. Did you mean 'auth.login' instead?)
当你有人试图访问/auth/secured
时, werkzeug.routing.BuildError (BuildError: Could not build url for endpoint 'login' with values ['next']. Did you mean 'auth.login' instead?)
作为一个未经身份验证的用户,因为我有这样编码:
@auth.route('/secured')
@ldap.login_required
def secured():
return 'Login Success! {0}'.format(session)
I'm using Flask Blueprints for my URL routing... I assumed this should work... 我正在使用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
The app directory is built as follows: app目录的构建如下:
testapp/
|
+ config.py
+ manage.py
+ app/
|
+ __init__.py
+ auth/
|
+ __init__.py
+ views.py
+ main/
|
+ __init__.py
+ views.py
As best I can tell, something is messed up in my blueprint registration; 我可以说,在我的蓝图注册中,有些东西搞砸了; however, I'm at a loss for where the error is.
但是,我对错误所在的地方感到茫然。
How do I make an unauthenticated user redirect correctly to the /auth/login
URL when they surf to /auth/secured
as an unauthenticated user? 如何将未经
/auth/secured
验证的用户作为未经/auth/secured
验证的用户进行/auth/secured
冲浪时,将其正确重定向到/auth/login
URL?
Looks like there are a couple of problems. 看起来有几个问题。
The first is a missing config parameter, LDAP_LOGIN_VIEW
. 第一个是缺少配置参数
LDAP_LOGIN_VIEW
。 The default is login
but since you don't have a view named 'login', you probably want to set this to be auth.login
in your config file: 默认为
login
但由于您没有名为“login”的视图,因此您可能希望在配置文件中将其设置为auth.login
:
LDAP_LOGIN_VIEW = 'auth.login'
The next issue is that your auth.login
doesn't handle a next parameter. 下一个问题是你的
auth.login
不处理下一个参数。 The next parameter tells the login function where to redirect after a successful login. 下一个参数告诉登录功能在成功登录后重定向的位置。 This might cause a problem since flask-simpleldap is passing a next parameter here .
这可能会导致问题,因为flask-simpleldap 在这里传递下一个参数 。
return redirect(url_for(current_app.config['LDAP_LOGIN_VIEW'],
next=request.path))
You can access the next parameter through the request object . 您可以通过请求对象访问下一个参数。
Example: 例:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.