【问题标题】:How to add csrf to flask app without wtforms?如何在没有 wtforms 的情况下将 csrf 添加到烧瓶应用程序?
【发布时间】:2020-09-11 16:57:06
【问题描述】:

我有一个简单的网络应用程序, 我想添加csrf保护。但是我没有看懂 Flask-WTF 提供的 csrf wrapper。我已经看过文档了。但还是不明白它是如何工作的。

我的问题是:

(1) 包裹app后,需要从路由中处理吗?还是烧瓶帮我处理?

(2) 如果不是自己怎么处理? (请举个例子)。

注意:我不想使用 wtf 表单,我想使用自定义标签进行输入。

app.py:

from flask import Flask, render_template
from flask_wtf.csrf import CSRFProtect

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
csrf = CSRFProtect(app)

@app.route('/', methods=['GET'])
def get_home():
    """Get home template"""
    return render_template('home.html')

@app.route('/', methods=['POST'])
def post_home():
    """Handle posted data and do stuff"""
    return

home.html(表单):

<form action="#" method="post">
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
    <input type="text" placeholder="Name">
    <button type="submit">
        Submit
    </button>
</form>

【问题讨论】:

    标签: html python-3.x flask csrf-token


    【解决方案1】:

    默认情况下,您无需担心自己验证它 - 您只需照常处理 POST 请求的其他字段。如果您在此处查看 CSRFProtect 类的 init_app 函数中的函数 csrf_protect()(第 202-225 行,https://github.com/lepture/flask-wtf/blob/master/flask_wtf/csrf.py),您可以查看将阻止 protect() 函数在给定的请求。

    【讨论】:

      猜你喜欢
      • 2013-03-16
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 2016-12-13
      • 1970-01-01
      • 2013-03-11
      相关资源
      最近更新 更多