Session vs JWT: When to Choose Token‑Based Authentication
This article explains the differences between authentication and authorization, compares session‑based and token‑based (JWT) authentication flows, details JWT structure and signing, and outlines the advantages, disadvantages, and suitable scenarios for each method.
Background
Authentication vs. Authorization: Authentication verifies a user's identity (e.g., logging in as user A), while authorization grants permissions after identity is confirmed (e.g., user A can modify data, user B can only read).
Because HTTP is stateless, each request lacks identity information, so a mechanism is needed to preserve login state across requests.
Session‑Based Authentication
The typical flow is:
User submits login credentials.
Server validates credentials, creates a session, and stores it in a database.
Server generates a sessionId and sends it to the client as a cookie.
On subsequent requests, the server checks the session ID in the database; if valid, the request is accepted.
When the user logs out, the session is destroyed on both client and server.
Token‑Based Authentication (JWT)
The most common token is a JSON Web Token (JWT):
User submits login credentials.
Server validates them and returns a signed token.
The token is stored on the client (e.g., local storage or cookie).
Subsequent HTTP requests include the token in the Authorization header.
Server decodes the JWT; if the token is valid, the request is accepted.
When the user logs out, the token is removed from the client; the server does not need to store any state.
JWT Structure
Header
The header describes the token type and signing algorithm, e.g.:
{
"typ": "JWT",
"alg": "HS256"
}Payload
The payload contains claims such as issuer, audience, subject, issued‑at, and expiration time, for example:
{
"iss": "John Wu JWT",
"iat": 1441593502,
"exp": 1441594722,
"aud": "www.example.com",
"sub": "[email protected]",
"from_user": "B",
"target_user": "A"
}These fields are Base64‑encoded and concatenated with a period.
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmcm9tX3VzZXIiOiJCIiwidGFyZ2V0X3VzZXIiOiJBIn0Signature
The concatenated header and payload are signed with a secret key using the HS256 algorithm, producing a signature that is appended to form the complete JWT:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmcm9tX3VzZXIiOiJCIiwidGFyZ2V0X3VzZXIiOiJBIn0.rSWamyAYwuHCo7IFAgd1oRpSP7nzL7BF5t7ItqpKViMDifferences, Advantages & Disadvantages
The main difference is where the state is stored: sessions keep state on the server, JWTs keep it on the client.
JWT Advantages
Scalability: No need for server‑side session sharing (e.g., database or Redis).
Statelessness: Aligns with RESTful principles; reduces database lookups.
JWT Disadvantages
Security: Payload is only Base64‑encoded, not encrypted; sensitive data should not be stored.
Performance: JWTs can become large, increasing request header size.
One‑time use: Tokens cannot be altered without re‑issuing; revocation requires additional mechanisms such as blacklists.
When to Use JWT
Short‑lived tokens.
One‑time actions, such as email activation links that must expire quickly.
For single sign‑on or session management, JWTs are less suitable unless server‑side state is introduced, which defeats their original purpose; in those cases, traditional session mechanisms are preferable.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
