หน้า 1 จากทั้งหมด 1

token flask python ใช่งานยังไงครับ

โพสต์แล้ว: 04/07/2017 2:34 pm
โดย nuattawoot
ผมทำการสร้าง เมธอดมาแล้ว เวลาดึงไปใช้งาน มันบอกว่า
UndefinedError: 'csrf_token' is undefined
ข้อที่ 2 ถ้า 1โปรแกรม เรามีหลายฟอร์ม เราต้องใส่ทุกฟอร์ม ใช้ไหมครับ
โค๊ดด่านยังไงคับผมลองแล้วไม่ได้

@app.before_request
def csrf_protect():
if request.method == "POST":
token = session.pop('_csrf_token', None)
if not token or token != request.form.get('_csrf_token'):
abort(403)
def generate_csrf_token():
if '_csrf_token' not in session:
session['_csrf_token'] = some_random_string()
return session['_csrf_token']

app.jinja_env.globals['csrf_token'] = generate_csrf_token
And then in your template:

<form method=post action="">
<input name=_csrf_token type=hidden value="{{ csrf_token() }}">

Re: token flask python ใช่งานยังไงครับ

โพสต์แล้ว: 05/07/2017 7:48 pm
โดย nuattawoot
ทำตามลิ้งนี้เลยครับ วิธีใช้ Flask Token
การใช้งาน Flask Token (ฟลาซค-โทเค็น) หรือ CSRF Protection (ซี-เอส-อา-เอฟ โพรเทคชัน) เพื่อเพิ่มความปลอดภัยให้กับเว็บไซต์
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E ... urity.html