ออกโทเคนเข้าถึงเมื่อเข้าสู่ระบบ
ลงลายมือชื่อ JWT และส่งคืนให้ไคลเอนต์
ออกโทเคนเข้าถึงเมื่อเข้าสู่ระบบ เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Login Handshake
Login is a trade: the client sends valid credentials, and your server hands back a signed access token it can use on every later call.
Install the Extension
The Flask-JWT-Extended library does the signing and checking for you. One pip install and you are ready to mint tokens.
pip install flask-jwt-extendedSet the Signing Key
Tokens are signed with a secret. Configure JWT_SECRET_KEY so only your server can produce valid tokens. Keep it out of source code.
app.config["JWT_SECRET_KEY"] = "change-me"Create the JWTManager
Wrap your app in a JWTManager. This one object hooks Flask-JWT-Extended into request handling so the helpers work.
from flask_jwt_extended import JWTManager
jwt = JWTManager(app)A Route to Log In
Add a POST route at /login. It will read the submitted username and password from the request body.
@app.post("/login")
def login():
...Verify Credentials First
Never skip the check: confirm the password matches your stored hash before issuing anything. No valid login, no token.
Mint the Token
Call create_access_token with an identity, usually the user id. That id becomes the token's subject claim.
from flask_jwt_extended import create_access_token
token = create_access_token(identity=user.id)Return It as JSON
Send the token back in a JSON body. The client stores it and replays it on protected calls. 🔑
return {"access_token": token}, 200Identity Drives Claims
The identity you pass is what later requests will read back. Keep it small and stable, like a numeric user id.
Add Extra Claims
Need a role or plan in the token? Pass additional_claims so the data rides along and is available without a database hit.
create_access_token(identity=uid, additional_claims={"role": "admin"})Fail Logins Cleanly
If credentials are wrong, return 401 Unauthorized and no token. Clear, consistent failures keep your API predictable.
return {"msg": "Bad credentials"}, 401Quick Check
Recall which call actually produces the token.
Recap
Verify credentials, then create_access_token with the user id and return it as JSON. Set JWT_SECRET_KEY and a JWTManager first. ✅
คำถามที่พบบ่อย
บทเรียน “ออกโทเคนเข้าถึงเมื่อเข้าสู่ระบบ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ออกโทเคนเข้าถึงเมื่อเข้าสู่ระบบ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ออกโทเคนเข้าถึงเมื่อเข้าสู่ระบบ”
ลงลายมือชื่อ JWT และส่งคืนให้ไคลเอนต์ คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ออกโทเคนเข้าถึงเมื่อเข้าสู่ระบบ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เซสชันเทียบกับโทเคนไร้สถานะ
- ออกโทเคนเข้าถึงเมื่อเข้าสู่ระบบ
- ปกป้องปลายทางด้วย jwt_required
- โทเคนรีเฟรชและวันหมดอายุ