Community程式設計與開發github.com

mindrally/jwt-security

基於最佳實踐的 JWT 認證實施指南,涵蓋 Token 建立、驗證與儲存的安全性。

jwt-security 是什麼?

此技能提供一套完整的 JWT (JSON Web Token) 身份驗證安全實施指南,適用於需要處理 Token 創建、驗證與儲存的開發人員。它強調遵循業界公認的安全最佳實踐,例如使用強簽名算法(如 RS256 或 HS256)、設定合理的 Token 過期時間(exp)、驗證發行者(iss)與受眾(aud)聲明、以及安全地儲存 Token(例如在 HTTP-only cookie 中,而非 localStorage)。此外,此指南可協助團隊建立統一的認證標準,減少因 Token 管理疏失導致的漏洞,並支援多種開發環境與平台。通過遵循這些規則,開發者能夠防止常見的攻擊(如 Token 偽造、重放攻擊),確保用戶身份認證環節的可靠性。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/mindrally/skills/tree/main/skills/jwt-security

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

jwt-security 是做什麼的?

基於最佳實踐的 JWT 認證實施指南,涵蓋 Token 建立、驗證與儲存的安全性。

概述

本指南提供關於 JSON Web Token (JWT) 身份驗證的安全實施建議。遵循這些建議有助於防止常見的安全漏洞,例如 Token 偽造、重放攻擊以及敏感資訊洩露。

核心原則

  1. 使用強簽名算法:建議使用 RS256(非對稱加密)或 HS256(對稱加密),並確保金鑰長度足夠且安全儲存。
  2. 設定合理的到期時間:在 Token 中設定 exp 聲明,並選擇較短的過期時間(例如 15 分鐘),減少 Token 被盜用的風險。
  3. 驗證所有必要聲明:必須驗證 Token 的簽名、iss(發行者)、aud(受眾)及 exp(過期時間)等關鍵聲明。
  4. 安全儲存 Token:避免將 Token 儲存在 localStorage 或 sessionStorage。建議使用 HTTP-only、Secure 及 SameSite 屬性設定的 Cookie 來防止跨站腳本攻擊 (XSS)。
  5. 使用 HTTPS:所有涉及 Token 傳輸的請求必須透過 HTTPS 進行,以防止中間人攻擊。

實作指引

建立 Token

import jwt
import datetime

payload = {
    "sub": "user123",
    "iat": datetime.datetime.utcnow(),
    "exp": datetime.datetime.utcnow() + datetime.timedelta(minutes=15),
    "iss": "my-app",
    "aud": "my-api"
}

token = jwt.encode(payload, "your-256-bit-secret", algorithm="HS256")

驗證 Token

try:
    decoded = jwt.decode(token, "your-256-bit-secret", algorithms=["HS256"], audience="my-api", issuer="my-app")
except jwt.ExpiredSignatureError:
    print("Token 已過期")
except jwt.InvalidTokenError:
    print("無效的 Token")

支援的平台

此指南適用於所有支援 JWT 的開發平台,包括但不限於 Node.js、Python、Java、Go 及 .NET。

參考資源

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

mindrally/accessibility-a11y

Implement web accessibility (a11y) best practices following WCAG guidelines to create inclusive, accessible user interfaces.

mindrally/chrome-extension-development

Expert guidelines for Chrome extension development with Manifest V3, covering security, performance, and best practices. Use when building browser extensions, creating popup UIs, implementing content scripts, working with Chrome APIs, managing extension permissions, or publishing to Chrome Web Store.

mindrally/computer-vision-opencv

Expert guidance for computer vision development using OpenCV, PyTorch, and modern deep learning techniques for image and video processing.

mindrally/data-analysis-jupyter

Expert guidance for data analysis, visualization, and Jupyter Notebook development with pandas, matplotlib, seaborn, and numpy.

mindrally/deep-learning-pytorch

Expert guidance for deep learning, transformers, diffusion models, and LLM development with PyTorch, Transformers, Diffusers, and Gradio.

mindrally/expo-react-native-typescript

Expert in Expo React Native TypeScript mobile development with best practices

mindrally/fastapi-python

Expert in FastAPI Python development with best practices for APIs and async operations

mindrally/framer-motion

Expert guidelines for building performant animations with Framer Motion/Motion library in React applications

mindrally/htmx

HTMX development guidelines for building dynamic web applications with minimal JavaScript using HTML attributes.

mindrally/machine-learning

Machine learning development with JAX, functional programming patterns, and high-performance computing.

mindrally/mysql-best-practices

MySQL development best practices for schema design, query optimization, and database administration

mindrally/nextjs-react-typescript

Expert in TypeScript, Node.js, Next.js App Router, React, Shadcn UI, Radix UI and Tailwind

mindrally/nextjs-typescript-tailwindcss-supabase

Full-stack Next.js 14 development with TypeScript, TailwindCSS, and Supabase for building production-ready web applications.

mindrally/odoo-development

Expert guidance for Odoo ERP development including Python ORM, XML views, and module architecture

mindrally/puppeteer-automation

Expert guidance for browser automation using Puppeteer with best practices for web scraping, testing, screenshot capture, and JavaScript execution in headless Chrome.

mindrally/redis-best-practices

Redis development best practices for caching, data structures, and high-performance key-value operations

mindrally/scrapy-web-scraping

Expert guidance for building web scrapers and crawlers using the Scrapy Python framework with best practices for spider development, data extraction, and pipeline management.

mindrally/scss-best-practices

SCSS/Sassy CSS best practices and coding guidelines for maintainable, scalable stylesheets

mindrally/typeorm

Guidelines for developing with TypeORM, a full-featured ORM for TypeScript and JavaScript supporting multiple databases

mindrally/web-scraping

Expert in web scraping and data extraction with Python tools

相關技能