โทเค็นเข้าถึง โทเค็นรีเฟรช และขอบเขต
ทำความเข้าใจโทเค็นที่ OAuth2 ออกให้ วิธีที่โทเค็นรีเฟรชช่วยให้เซสชันทำงานต่อ และวิธีที่ขอบเขตจำกัดสิ่งที่โทเค็นทำได้
โทเค็นเข้าถึง โทเค็นรีเฟรช และขอบเขต เป็นบทเรียน OAuth2 & OpenID Connect Deep Dive ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน OAuth2 & OpenID Connect Deep Dive และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Tokens Are the Currency of OAuth2
After a successful flow, OAuth2 hands the client a token instead of the user's credentials. The client presents this token to the resource server to access protected data.
The Access Token
An access token is a short-lived credential proving the client may call an API on the user's behalf. It is sent on each request, usually in the Authorization header.
GET /api/profile
Authorization: Bearer eyJhbGciOi...Why Tokens Expire
Access tokens are deliberately short-lived (minutes to an hour). If one leaks, the window of misuse is small. But forcing the user to re-login constantly would be painful.
The Refresh Token
A refresh token is a longer-lived credential the client uses to obtain new access tokens without bothering the user. It is stored securely and never sent to APIs.
Refreshing in Practice
When the access token expires, the client posts the refresh token to the token endpoint and receives a fresh access token.
POST /oauth/token
grant_type=refresh_token
&refresh_token=def502...
&client_id=my-appWhat Scopes Are
Scopes declare exactly what a token may do, such as read:profile or write:orders. They implement least privilege at the token level.
scope=read:profile write:ordersRequesting Scopes
The client requests scopes during authorization. The user consents, and the issued token is limited to the granted scopes, no more.
GET /authorize?response_type=code
&client_id=my-app
&scope=read:profile
&redirect_uri=https://app/cbEnforcing Scopes
The resource server checks that the token carries the scope an endpoint requires. A token with only read:profile is rejected from a write endpoint.
if (!token.getScopes().contains("write:orders")) {
return forbidden();
}Reference vs Self-Contained
An access token may be a random reference the server looks up, or a self-contained JWT carrying claims the server validates by signature without a lookup.
Revoking Tokens
Refresh tokens can be revoked to end a session, for example on logout or suspected compromise. This invalidates the ability to mint new access tokens.
POST /oauth/revoke
token=def502...
&token_type_hint=refresh_tokenToken Storage Matters
Store tokens carefully: never in plain localStorage for sensitive apps, keep refresh tokens server-side or in secure storage, and always use HTTPS in transit.
Quick Check
An access token has expired but you do not want the user to log in again. What does the client use to get a new one?
Recap
You learned how OAuth2 tokens work:
- Access tokens are short-lived and sent to APIs
- Refresh tokens silently obtain new access tokens
- Scopes enforce least privilege per token
- Tokens can be reference-based or JWTs
- Revocation and secure storage protect sessions
These concepts underpin every OAuth2 grant type you will study next.
คำถามที่พบบ่อย
บทเรียน “โทเค็นเข้าถึง โทเค็นรีเฟรช และขอบเขต” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “โทเค็นเข้าถึง โทเค็นรีเฟรช และขอบเขต” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส OAuth2 & OpenID Connect Deep Dive ให้อัปเกรดเป็น CoddyKit PRO คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “โทเค็นเข้าถึง โทเค็นรีเฟรช และขอบเขต”
ทำความเข้าใจโทเค็นที่ OAuth2 ออกให้ วิธีที่โทเค็นรีเฟรชช่วยให้เซสชันทำงานต่อ และวิธีที่ขอบเขตจำกัดสิ่งที่โทเค็นทำได้ คุณปฏิบัติ OAuth2 & OpenID Connect Deep Dive ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน OAuth2 & OpenID Connect Deep Dive หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน OAuth2 & OpenID Connect Deep Dive บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “โทเค็นเข้าถึง โทเค็นรีเฟรช และขอบเขต” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน OAuth2 & OpenID Connect Deep Dive นี้ได้ไหม
ได้ บทเรียน OAuth2 & OpenID Connect Deep Dive ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- OAuth2: โพรโทคอลการมอบสิทธิ์
- บทบาทและคำศัพท์ของ OAuth2
- ภาพรวมประเภทการมอบสิทธิ์หลัก
- โทเค็นเข้าถึง โทเค็นรีเฟรช และขอบเขต