Next.js 15 Fullstack Web Apps · 课时

会话管理与 JWT

理解会话的管理方式,以及 JSON Web Token(JWT)如何用于安全的用户身份验证。

第 2 / 4 课11 个步骤

会话管理与 JWT 是 CoddyKit 上的免费 Next.js 15 Fullstack Web Apps 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Next.js 15 Fullstack Web Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Next.js 15 Fullstack Web Apps 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What are Sessions?

On the web, a "session" is a way for a server to remember a user over multiple requests. Think of it like a conversation with a short-term memory.

Since HTTP requests are stateless (each request is independent), sessions help maintain context about who you are and what you're doing.

Why We Need Sessions

Imagine you log into an online store. Without a session, every time you click a new product or add an item to your cart, the server would forget you're logged in!

Sessions link together related requests from the same user, allowing for a personalized and continuous experience across your application.

Traditional Sessions & Cookies

Traditionally, sessions involve the server creating a unique "session ID" for a user after login. This ID is stored on the server.

The server then sends this session ID to the browser, usually in a cookie. The browser automatically sends this cookie back with every subsequent request, allowing the server to identify the user.

Meet JSON Web Tokens (JWTs)

JSON Web Tokens (pronounced "jot") offer a modern alternative to traditional sessions. They are self-contained, compact, and digitally signed pieces of information.

Instead of a server storing session data, JWTs store user-specific information directly within the token itself.

JWT Structure: Header

A JWT consists of three parts, separated by dots: Header, Payload, and Signature.

The Header usually contains two fields:

  • alg: The algorithm used for signing the token (e.g., HMAC SHA256 or RSA).
  • typ: The type of the token, which is usually "JWT".
{
  "alg": "HS256",
  "typ": "JWT"
}

JWT Structure: Payload

The Payload contains "claims" – statements about an entity (like a user) and additional data. Claims can be:

  • Registered Claims: Standard claims like iss (issuer), sub (subject), exp (expiration time).
  • Public Claims: Custom claims defined by you, but registered in the IANA JWT Registry.
  • Private Claims: Custom claims agreed upon by parties, not publicly registered.
{
  "sub": "1234567890",
  "name": "Jane Doe",
  "admin": true,
  "iat": 1516239022,
  "exp": 1516242622
}

JWT Structure: Signature

The Signature is created by taking the encoded Header, the encoded Payload, a secret key, and the algorithm specified in the header, then signing them.

This signature is crucial! It verifies that the sender of the JWT is who it says it is and that the message hasn't been tampered with along the way.

How JWTs Work in Practice

Here's a typical flow:

  1. User logs in with credentials.
  2. Server verifies credentials and creates a JWT.
  3. Server sends the JWT back to the client.
  4. Client stores the JWT (e.g., in local storage or a cookie).
  5. For subsequent requests, the client sends the JWT (usually in an Authorization header).
  6. Server verifies the JWT's signature and expiration before processing the request.

JWT Pros and Cons

Benefits:

  • Stateless: Servers don't need to store session data, improving scalability.
  • Decentralized: Tokens can be verified by any server that has the secret key.
  • Mobile-friendly: Easy to use across different clients (web, mobile apps).

Considerations:

  • Storage: Where to securely store tokens on the client side.
  • Revocation: Harder to revoke an active token before it expires.
  • Size: Can be larger than a simple session ID.

Quick Check on JWTs

Test your understanding of JSON Web Tokens!

Session & JWT Recap

Great job! In this lesson, you learned about:

  • The purpose of web sessions in maintaining user context.
  • How traditional sessions use server-side storage and cookies.
  • The structure (Header, Payload, Signature) and function of JSON Web Tokens (JWTs).
  • The workflow of using JWTs for authentication.
  • Key benefits and considerations when choosing JWTs for your applications.

Next, we'll dive into protecting routes and API endpoints using Next.js Middleware!

免费开始

用 AI 导师学习 TypeScript — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「会话管理与 JWT」课时是免费的吗?

是的 — 「会话管理与 JWT」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Next.js 15 Fullstack Web Apps 课程的其余内容,请升级到 CoddyKit PRO。 Next.js 15 Fullstack Web Apps 课程共包含 4 节课。

「会话管理与 JWT」这节课中我会学到什么?

理解会话的管理方式,以及 JSON Web Token(JWT)如何用于安全的用户身份验证。 你通过在浏览器中直接运行的动手代码来练习 Next.js 15 Fullstack Web Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Next.js 15 Fullstack Web Apps 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Next.js 15 Fullstack Web Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「会话管理与 JWT」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Next.js 15 Fullstack Web Apps 课中编写并运行代码吗?

能。每节 Next.js 15 Fullstack Web Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 集成 NextAuth.js
  2. 会话管理与 JWT
  3. 中间件与访问控制
  4. 基于角色的访问控制(RBAC)
← 返回 Next.js 15 Fullstack Web Apps