最近我开始在我的应用程序中使用 flask_session 扩展,我也出于安全目的使用flask_wtf.csrf扩展令牌。
现在,在flask_session之前应用程序运行良好,但是当我安装flask_session并在我的config.py中配置它的参数时,如果我尝试登录或注册应用程序给我400 Bad Request The CSRF session token is missing.
,事实上,我在我的所有请求中都包含了csrf_token,包括还有ajax请求。
会话存储类型为sqlalchemy
:
SESSION_COOKIE_NAME = 'booking'
SESSION_TYPE = 'sqlalchemy'
PERMANENT_SESSION_LIFETIME = timedelta(seconds=120)
SESSION_SQLALCHEMY_TABLE = 'sessions'
SESSION_KEY_PREFIX = 'booking'
我忘了提,我注意到如果我将类型更改为“文件系统”,错误不会出现。
这是flask_session 文档以获取更多信息。
登录模板:
<form class="login_form" role="form" action="{{url_for('client.login')}}" method="post">
{{ login_form.hidden_tag() }}
{% if login_form.telephone.errors %}
{% for e in login_form.telephone.errors %}
<p class="text-error">{{e}}</p>
{% endfor %}
{% endif %}
{{login_form.telephone(class="_bit mat_input", id="form-phone-number", placeholder="You login")}}
{% if login_form.password.errors %}
{% for e in login_form.password.errors %}
<p class="text-error">{{e}}</p>
{% endfor %}
{% endif %}
{{login_form.password(class="_bit mat_input", id="form-password", placeholder="Your password")}}
<a href="" class="login_button">
<button type="submit" class="mat_button _bib">Login</button>
</a>
<div class="_dis_b rfb">
<div class="remember_row">
<label for="remember" class="remember">remember me</label>
{{login_form.remember_me(id="remember", class="check")}}
</div>
<div class="forgot_row">
<a href="javascript:void(0);" class="forgot">Forgot password?</a>
</div>
</div>
</form>
客户端views.py:
@client_route.route('/client/cat-<dir_code>/login', methods=['GET','POST'])
def login():
form_login = ClientLogin()
if request.method == 'GET' and request.args.get('next'):
session['next'] = request.args.get('next')
if form_login.validate_on_submit():
user = Client.query.filter_by(
tele = form_login.telephone.data
).first()
if user:
if check_password_hash(user.password, form_login.password.data):
session['client_logged_in'] = user.name
session['client_family'] = user.family
session['client_image'] = user.image
session['client_phone'] = user.tele
if 'next' in session:
next = session.get('next')
session.pop('next')
return redirect(next+'?current_user='+session.get('client_logged_in')+'+'+session.get('client_family'))
else:
return redirect(url_for('client.lenta', dir_code=g.current_directory)+'?current_user='+session.get('client_logged_in')+'+'+session.get('client_family'))
else:
flash('Invalid credentials.', 'danger')
return redirect(url_for('client.login', dir_code=g.current_directory))
else:
flash('Invalid credentials.', 'danger')
return redirect(url_for('client.login', dir_code=g.current_directory))
return render_template('client/login.html', user=user, form_login=form_login)