0Pricing
Flask Academy · บทเรียน

ปกป้องปลายทางด้วย jwt_required

ตรวจสอบโทเคนในทุกการเรียก API

ปกป้องปลายทางด้วย jwt_required เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Guarding a Route

A token is only useful if some routes demand it. The jwt_required decorator turns any view into a members-only endpoint. 🔒

Apply the Decorator

Stack @jwt_required() under your route. Now the view only runs when a valid, unexpired token arrives.

@app.get("/profile")
@jwt_required()
def profile():
    ...

Where the Token Goes

Clients send the token in the Authorization header using the Bearer scheme. Flask-JWT-Extended reads it from there automatically.

Authorization: Bearer <your-token>

Automatic Rejection

No token, or a bad one? The decorator stops the request before your code runs and returns a 401. You write zero checking logic.

Who Is Calling

Inside a guarded view, call get_jwt_identity to read the identity you signed at login, usually the user id.

from flask_jwt_extended import get_jwt_identity
uid = get_jwt_identity()

Read the Full Claims

Need extra claims like a role? get_jwt returns the whole payload as a dict so you can branch on what is inside.

from flask_jwt_extended import get_jwt
role = get_jwt().get("role")

Use the Identity

With the user id in hand, load that user and serve their data. The token told you who, so the rest is normal Flask. 🙂

user = User.query.get(get_jwt_identity())

Optional Protection

Want a page that adapts whether or not someone is logged in? Use optional=True so the view runs either way.

@jwt_required(optional=True)

Customize the Error

Register an unauthorized_loader to return a friendly JSON message instead of the default when a token is missing.

@jwt.unauthorized_loader
def missing(reason):
    return {"msg": reason}, 401

Expired Tokens

An expired token is also rejected by the decorator. The client must log in again or refresh to keep going, which keeps stolen tokens short-lived.

Never Trust the Body

Read the caller from the token, never from a user id in the request body. The signature is what makes the identity trustworthy.

Quick Check

Recall how a guarded view learns who is calling.

Recap

Add @jwt_required() to guard a route, send the token as a Bearer header, and read the caller with get_jwt_identity. 🔐

คำถามที่พบบ่อย

บทเรียน “ปกป้องปลายทางด้วย jwt_required” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ปกป้องปลายทางด้วย jwt_required” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ปกป้องปลายทางด้วย jwt_required”

ตรวจสอบโทเคนในทุกการเรียก API คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “ปกป้องปลายทางด้วย jwt_required” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม

ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เซสชันเทียบกับโทเคนไร้สถานะ
  2. ออกโทเคนเข้าถึงเมื่อเข้าสู่ระบบ
  3. ปกป้องปลายทางด้วย jwt_required
  4. โทเคนรีเฟรชและวันหมดอายุ
← กลับไปที่ Flask Academy