惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 叶小钗
小众软件
小众软件
博客园 - Franky
D
Docker
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
宝玉的分享
宝玉的分享
C
Check Point Blog
B
Blog
V
V2EX
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
The Cloudflare Blog
博客园 - 聂微东
博客园_首页
Engineering at Meta
Engineering at Meta

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Cookie, Session và Token-based authentication
Đạt Trương T · 2026-05-10 · via DEV Community

Đạt Trương Thành

1. Phân biệt Authentication và Authorization

Rất nhiều lập trình viên mới hay nhầm lẫn hai khái niệm này:

  • Authentication (Xác thực): Ví dụ: Hành động bạn nhập Username và Password để đăng nhập vào hệ thống.

  • Authorization (Phân quyền): Ví dụ: Sau khi đăng nhập thành công, hệ thống kiểm tra xem bạn là Admin hay User thường, bạn có quyền xóa bài viết hay chỉ được xem.

2. Giao thức HTTP và vấn đề "Stateless"

  • Giao thức HTTP bản chất là Stateless. Nghĩa là server xử lý xong một request là sẽ forget luôn client đó là ai.

  • Nếu không có cơ chế lưu trữ lại trạng thái, mỗi lần bạn bấm sang một trang mới trên website, hệ thống sẽ lại bắt bạn đăng nhập lại. Để giải quyết vấn đề này, người ta sinh ra Cookie, SessionToken.

3. Cơ chế Cookie và Session

Cookie:

  • Là một đoạn dữ liệu nhỏ (khoảng vài KB) được Server yêu cầu Client (browse) lưu trữ lại ở dưới máy của người dùng.

  • Mỗi khi Client gửi một request mới lên Server, nó sẽ tự động đính kèm các Cookie này theo. Nhờ đó, Server biết được request này đến từ ai.

Session:

  • Lưu toàn bộ thông tin nhạy cảm ở phía Client (bằng Cookie) là rất nguy hiểm (dễ bị hack). Do đó, người ta sinh ra Session.

  • Cách hoạt động: Khi bạn đăng nhập thành công, Server sẽ tạo ra một vùng nhớ chứa thông tin của bạn (gọi là Session) và sinh ra một Session ID. Server chỉ gửi cái Session ID này về cho Client để lưu vào Cookie.

  • Các lần request sau, Client chỉ gửi Session ID lên. Server lấy Session ID này dò trong bộ nhớ/DB Session của mình để biết bạn là ai.

4. Token-based Authentication

  • Nhược điểm của Session: Khi hệ thống scale lên nhiều Server, Server A lưu Session của bạn nhưng Server B thì không. Nếu request của bạn bị điều hướng sang Server B, bạn sẽ bị bắt đăng nhập lại.

  • Giải pháp Token (JWT - JSON Web Token):

    • Thay vì lưu trạng thái ở Server, sau khi đăng nhập thành công, Server sẽ gom thông tin của bạn (như ID, Role), mã hóasign bằng một khóa bí mật, tạo thành một chuỗi gọi là Token.
    • Server gửi Token này cho Client lưu lại (thường lưu ở Local Storage hoặc Cookie).
    • Lần sau gửi request, Client đính kèm Token này vào Header (thường là Authorization: Bearer <token>).
    • Server không cần tìm trong bộ nhớ nữa, chỉ cần dùng khóa bí mật để giải mã và kiểm tra chữ ký của Token là biết bạn là ai và có quyền gì.
  • Ưu điểm: Cực kỳ phù hợp cho Web API, các ứng dụng Mobile, và hệ thống Microservices vì nó hoàn toàn "Stateless" (Server không cần tốn RAM để nhớ người dùng).