0Pricing
Spring Security 6 & JWT Authentication · บทเรียน

โครงสร้างและข้อมูลอ้างสิทธิ์ของ JWT

แยกทำความเข้าใจสามส่วนของ JWT ได้แก่ Header, Payload และ Signature รวมถึงข้อมูลอ้างสิทธิ์มาตรฐานและแบบกำหนดเอง

โครงสร้างและข้อมูลอ้างสิทธิ์ของ JWT เป็นบทเรียน Spring Security 6 & JWT Authentication ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Security 6 & JWT Authentication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน

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

What's Inside a JWT?

JSON Web Tokens (JWTs) might look like long, random strings, but they have a very clear and organized structure.

A JWT is composed of three distinct parts, each separated by a dot (.):

  • Header
  • Payload
  • Signature

Think of it like a sealed letter: the header is information about the letter itself, the payload is the message inside, and the signature is the wax seal proving its authenticity.

Part 1: The Header

The Header is the first part of a JWT. It's a JSON object that contains metadata about the token itself, primarily telling us what algorithm was used to sign the token.

It typically includes two key elements:

  • alg (Algorithm): Specifies the cryptographic algorithm used for signing the token (e.g., HS256 for HMAC SHA-256, RS256 for RSA SHA-256).
  • typ (Type): Indicates the type of token, which is almost always JWT.

Header Example

Here's what a typical JWT header looks like as a JSON object:

{
  "alg": "HS256",
  "typ": "JWT"
}

Before being included in the JWT string, this JSON object is Base64Url encoded. This process converts the JSON into a web-safe string.

Part 2: The Payload (Claims)

The Payload is the second part of the JWT and is arguably the most important. It's also a JSON object, but this one contains the actual data, known as "claims."

Claims are statements about an entity (usually the user) and additional data. They are essentially key-value pairs that carry information such as:

  • User ID
  • User roles or permissions
  • Token expiration time

Standard Claims - The Basics

JWTs define a set of "standard claims" that are recommended for common use. While optional, using them helps ensure interoperability.

Some common standard claims include:

  • iss (Issuer): Identifies the principal that issued the JWT (e.g., auth.example.com).
  • sub (Subject): Identifies the principal that is the subject of the JWT (e.g., a user ID like user123).
  • exp (Expiration Time): The time after which the JWT MUST NOT be accepted for processing. It's a Unix timestamp.
  • iat (Issued At): The time at which the JWT was issued. Also a Unix timestamp.
  • aud (Audience): Identifies the recipients that the JWT is intended for (e.g., api.example.com).

Custom Claims - Your Data

In addition to standard claims, you can include any custom claims in the payload that are relevant to your application.

This allows you to store application-specific data directly within the token, such as:

  • User-specific roles (e.g., admin, editor)
  • Permissions (e.g., read:product, write:order)
  • Unique identifiers specific to your system

Keep custom claims concise to minimize the overall token size, which helps with performance.

Payload Example

Here's an example of a JWT payload containing both standard and custom claims:

{
  "sub": "user123",
  "name": "Alice Smith",
  "roles": ["admin", "editor"],
  "iat": 1678886400,
  "exp": 1678890000
}

Like the header, this JSON object is also Base64Url encoded before becoming part of the full JWT string.

Part 3: The Signature

The Signature is the third and final part of a JWT. It's critical for security, as it serves two main purposes:

  1. Integrity: Verifies that the token hasn't been tampered with since it was issued.
  2. Authenticity: Confirms that the token was indeed created by the expected issuer.

Without a valid signature, the token should be considered invalid and untrustworthy.

How the Signature is Made

The signature is created by taking the Base64Url encoded header, the Base64Url encoded payload, and a secret key, then running them through the cryptographic algorithm specified in the header.

Conceptually, it works like this:

signature = Algorithm( 
  Base64Url(header) + "." + 
  Base64Url(payload), 
  secret_key
)

The resulting signature is then also Base64Url encoded and appended to the JWT string, completing its three-part structure.

Quick Check: JWT Claims

Test your knowledge on JWT claims!

Recap: The JWT Blueprint

Great job! You now understand the fundamental structure of a JSON Web Token.

  • The Header contains metadata about the token, including the signing algorithm and type.
  • The Payload carries the actual data in the form of "claims," which can be standard (like iss, sub, exp) or custom.
  • The Signature is a cryptographic hash that ensures the token's integrity and authenticity, preventing tampering and verifying the sender.

Understanding these three distinct parts is crucial for effectively working with and securing applications using JWTs.

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

บทเรียน “โครงสร้างและข้อมูลอ้างสิทธิ์ของ JWT” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “โครงสร้างและข้อมูลอ้างสิทธิ์ของ JWT”

แยกทำความเข้าใจสามส่วนของ JWT ได้แก่ Header, Payload และ Signature รวมถึงข้อมูลอ้างสิทธิ์มาตรฐานและแบบกำหนดเอง คุณปฏิบัติ Spring Security 6 & JWT Authentication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Security 6 & JWT Authentication หรือไม่

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

บทเรียน “โครงสร้างและข้อมูลอ้างสิทธิ์ของ JWT” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Spring Security 6 & JWT Authentication นี้ได้ไหม

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

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

  1. ทำความเข้าใจ JSON Web Tokens
  2. โครงสร้างและข้อมูลอ้างสิทธิ์ของ JWT
  3. การลงลายมือชื่อและตรวจสอบ JWT
  4. กฎการหมดอายุและการตรวจสอบ JWT
← กลับไปที่ Spring Security 6 & JWT Authentication