0Pricing
Flask Academy · درس

حماية نقاط النهاية باستخدام jwt_required

تحقق من الرمز المميز في كل استدعاء لـ API.

حماية نقاط النهاية باستخدام jwt_required درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «حماية نقاط النهاية باستخدام jwt_required»؟

تحقق من الرمز المميز في كل استدعاء لـ API. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «حماية نقاط النهاية باستخدام jwt_required»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. الجلسات مقابل الرموز عديمة الحالة
  2. إصدار رموز الوصول عند تسجيل الدخول
  3. حماية نقاط النهاية باستخدام jwt_required
  4. رموز التحديث وانتهاء الصلاحية
← العودة إلى Flask Academy