Mr.*_*oid 7 html flask python-3.x csrf-token
我有一个简单的网络应用程序,\n并且我想添加 csrf 保护。但我不理解 Flask-WTF 提供的 csrf 包装器。我已经看过文档了。但仍然不明白\xe2\x80\x99 是如何工作的。
\n我的问题是:
\n(1)应用程序打包后,是否需要从路由中处理?或者烧瓶帮我处理这个?
\n(2)如果不是,我自己该如何处理?(请提供示例)。
\n注意:我不想使用 wtf 表单,我想使用自定义标签进行输入。
\n应用程序.py:
\nfrom flask import Flask, render_template\nfrom flask_wtf.csrf import CSRFProtect\n\napp = Flask(__name__)\napp.config['SECRET_KEY'] = 'secret'\ncsrf = CSRFProtect(app)\n\n@app.route('/', methods=['GET'])\ndef get_home():\n """Get home template"""\n return render_template('home.html')\n\n@app.route('/', methods=['POST'])\ndef post_home():\n """Handle posted data and do stuff"""\n return\nRun Code Online (Sandbox Code Playgroud)\nhome.html(表单):
\n<form action="#" method="post">\n <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>\n <input type="text" placeholder="Name">\n <button type="submit">\n Submit\n </button>\n</form>\nRun Code Online (Sandbox Code Playgroud)\n
小智 1
默认情况下,您无需担心自己验证它 - 您只需照常处理 POST 请求的其他字段即可。如果您查看此处类的函数csrf_protect()内的函数(第 202-225 行,https://github.com/lepture/flask-wtf/blob/master/flask_wtf/csrf.py),您可以查看以下内容:将在给定请求之前停止正在运行的函数。init_appCSRFProtectprotect()