def _get_csrf_token(self):
# find the token in the form data
field_name = current_app.config['WTF_CSRF_FIELD_NAME']
base_token = request.form.get(field_name)
if base_token:
return base_token
# if the form has a prefix, the name will be {prefix}-csrf_token
for key in request.form:
if key.endswith(field_name):
csrf_token = request.form[key]
if csrf_token:
return csrf_token
# find the token in the headers
for header_name in current_app.config['WTF_CSRF_HEADERS']:
csrf_token = request.headers.get(header_name)
if csrf_token:
return csrf_token
return None
这取决于你的 token 存放位置.
示例1. 放在表单中
那么后端用
request.form['csrf_token']
可获取.示例2. 放在 Ajax Header 中
那么后端可用
request.headers['X-CSRFToken']
.或者参考 flask CSRF 的源代码实现
https://github.com/lepture/fl...