Mr Dk.'s BlogMr Dk.'s Blog
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 📝 Notes
    • Algorithm
      • Algorithm - Bloom Filter
      • Algorithm - Disjoint Set
      • Algorithm - Fast Power
      • Algorithm - KMP
      • Algorithm - Monotonic Stack
      • Algorithm - RB-Tree
      • Algorithm - Regular Expression
      • Algorithm - Sliding Window
      • Online Judge - I/O
    • C++
      • C++ - Const
      • C++ File I/O
      • C++ - Object Layout
      • C++ - Operator Overload
      • C++ - Polymorphism
      • C++ STL algorithm
      • C++ STL map
      • C++ STL multimap
      • C++ STL priority_queue
      • C++ STL set
      • C++ STL string
      • C++ STL unordered_map
      • C++ STL vector
      • C++ - Smart Pointer
      • C++ - Template & Genericity
    • Compiler
      • ANTLR - Basic
      • Compiler - LLVM Architecture
      • Compiler - Multi-version GCC
    • Cryptography
      • Cryptography - Certbot
      • Cryptography - Digital Signature & PKCS #7
      • Cryptography - GPG
      • Cryptography - JWT
      • Cryptography - Keystore & Certificates
      • Cryptography - OAuth 2.0
      • Cryptography - Java 实现对称与非对称加密算法
      • Cryptography - TLS
    • DevOps
      • DevOps - Travis CI
    • Docker
      • Docker - Image & Storage Management
      • Docker - Image
      • Docker - Libcontainer
      • Docker - Multi-Arch Image
      • Docker - Multi-Stage Build
      • Docker - Network
      • Docker - Orchestration & Deployment
      • Docker - Overview
      • Docker - Service Building
      • Docker - Volume & Network Usage
      • Docker - Volume
      • Linux - Control Group
      • Linux - Namespace
    • Git
      • Git - Branch & Merge
      • Git - Cached
      • Git - Cherry Pick
      • Git - Commit
      • Git - Patch
      • Git - Proxy
      • Git - Rebase
      • Git - Reset
      • Git - Stash
      • Git - Theme for Git-Bash
    • Java
      • JVM - Synchronized
      • JVM - Volatile
      • Java - Annotation 注解
      • Java - BIO & NIO
      • Java - Class Path
      • Java - Condition and LockSupport
      • Java - Current Timestamp
      • Java - Deep Copy
      • Java - 运行环境配置
      • Java - Equals
      • Java - Exporting JAR
      • Java - Javadoc
      • Java - Lock
      • Java - Maven 项目构建工具
      • Java - References
      • Java - Reflection Mechanism
      • Java - String Split
      • Java - Thread Pool
      • Java - Thread
      • Tomcat - Class Loader
      • Tomcat - Container
    • Linux
      • addr2line
      • cut
      • df
      • du
      • fallocate
      • find
      • fio
      • grep
      • groupadd
      • gzip
      • head / tail
      • hexdump
      • iostat
      • iotop
      • kill
      • ldd
      • lsof
      • ltrace / strace
      • mpstat
      • netstat
      • nm
      • pidstat
      • pmap
      • readlink
      • readlink
      • rpm2cpio / rpm2archive
      • sort
      • tee
      • uniq
      • useradd
      • usermod
      • watch
      • wc
      • which
      • xargs
    • MS Office
      • MS Office - Add-in Dev
      • MS Office - Application
    • MySQL
      • InnoDB - Architecture
      • InnoDB - Backup
      • InnoDB - Checkpoint
      • InnoDB - Critical Features
      • InnoDB - Files
      • InnoDB - Index
      • InnoDB - Insert Buffer
      • InnoDB - Lock
      • InnoDB - Partition Table
      • InnoDB - Table Storage
      • MySQL - Server Configuration
      • MySQL - Storage Engine
    • Network
      • Network - ARP
      • Network - FTP
      • Network - GitHub Accelerating
      • HTTP - Message Format
      • HTTP - POST 提交表单的两种方式
      • Network - Proxy Server
      • Network - SCP
      • Network - SSH
      • Network - TCP Congestion Control
      • Network - TCP Connection Management
      • Network - TCP Flow Control
      • Network - TCP Retransmission
      • Network - Traceroute
      • Network - V2Ray
      • Network - WebSocket
      • Network - Windows 10 Mail APP
      • Network - frp
    • Operating System
      • Linux - Kernel Compilation
      • Linux - Multi-OS
      • Linux - Mutex & Condition
      • Linux - Operations
      • Linux: Package Manager
      • Linux - Process Manipulation
      • Linux - User ID
      • Linux - Execve
      • OS - Compile and Link
      • OS - Dynamic Linking
      • OS - ELF
      • Linux - Image
      • OS - Loading
      • OS - Shared Library Organization
      • OS - Static Linking
      • Syzkaller - Architecture
      • Syzkaller - Description Syntax
      • Syzkaller - Usage
      • Ubuntu - Desktop Recover (Python)
      • WSL: CentOS 8
    • Performance
      • Linux Performance - Perf Event
      • Linux Performance - Perf Record
      • Linux Performance - Perf Report
      • Linux Performance - Flame Graphs
      • Linux Performance - Off CPU Analyze
    • PostgreSQL
      • PostgreSQL - ANALYZE
      • PostgreSQL - Atomics
      • PostgreSQL - CREATE INDEX CONCURRENTLY
      • PostgreSQL - COPY FROM
      • PostgreSQL - COPY TO
      • PostgreSQL - Executor: Append
      • PostgreSQL - Executor: Group
      • PostgreSQL - Executor: Limit
      • PostgreSQL - Executor: Material
      • PostgreSQL - Executor: Nest Loop Join
      • PostgreSQL - Executor: Result
      • PostgreSQL - Executor: Sequential Scan
      • PostgreSQL - Executor: Sort
      • PostgreSQL - Executor: Unique
      • PostgreSQL - FDW Asynchronous Execution
      • PostgreSQL - GUC
      • PostgreSQL - Locking
      • PostgreSQL - LWLock
      • PostgreSQL - Multi Insert
      • PostgreSQL - Plan Hint GUC
      • PostgreSQL - Process Activity
      • PostgreSQL - Query Execution
      • PostgreSQL - Spinlock
      • PostgreSQL - Storage Management
      • PostgreSQL - VFD
      • PostgreSQL - WAL Insert
      • PostgreSQL - WAL Prefetch
    • Productivity
      • LaTeX
      • Venn Diagram
      • VuePress
    • Solidity
      • Solidity - ABI Specification
      • Solidity - Contracts
      • Solidity - Expressions and Control Structures
      • Solidity - Layout and Structure
      • Solidity - Remix IDE
      • Solidity - Slither
      • Solidity - Types
      • Solidity - Units and Globally Available Variables
    • Vue.js
      • Vue.js - Environment Variable
    • Web
      • Web - CORS
      • Web - OpenAPI Specification
    • Wireless
      • Wireless - WEP Cracking by Aircrack-ng
      • Wireless - WPS Cracking by Reaver
      • Wireless - wifiphisher

Cryptography - JWT

Created by : Mr Dk.

2020 / 03 / 09 22:18

Ningbo, Zhejiang, China


Introduction

JWT 全名 JSON Web Token,是一个开放标准 (RFC 7519)。其定义了一种 紧凑 且 自包含 的模式,通过 JSON 对象,保证双方通信的安全。

主要应用场景在于签发 token。本来,服务器需要通过会话 id 来区分用户;而使用 token 后,服务器将不再需要维护会话状态,而是根据 token 中的用户信息对用户进行区分。同时,在有效期内,token 是用户访问的凭据 - 用于替代账号密码,减少数据库的访问次数。

Token 分为两类:

  • 签名 token:用于验证其中自包含信息的 完整性 (即未经篡改)
  • 加密 token:用于对其它实体隐藏其中的信息

使用场景:

  • 授权
    • 用户登录成功后,发放 token
    • 之后用户访问所有的受限资源都需要携带这个 token,无需再进行认证
    • 开销小,可以在跨域场景中方便使用
  • 信息交换
    • 用于使用了公私钥签名,验证信息是否遭到篡改

JWT 支持用一个密钥 + HMAC 算法的签名方式,也支持 RSA 或 ECDSA 的公私钥签名方式。

Token Structure

Token 由三部分构成,这三部分用 . 分隔:

  • Header
  • Payload
  • Signature

即 <header>.<payload>.<signature>。

Header

头部包含两部分:

  • Token 的类型 - JWT
  • 签名算法 - HMAC SHA 256 / RSA
{
  "alg": "HS256",
  "typ": "JWT"
}

然后,这个 JSON 对象被 Base64Url 编码后,成为 token 的第一部分。

Base64Url 与 Base64 有细微差别 - 个别字符的编码不一样。

主要是为了保证产生的 token 能够在 URL 中传输。

Payload

负载中包含了用户信息以及其它数据的声明,通常包含三种类型:

  • 注册声明:不强制但推荐使用,是一些预定义的字段
    • iss (issuer) 发行者
    • exp (expiration time) 过期时间
    • sub (subject) 主题
    • aud (audience) 观众
  • 公共声明:可以随意定义,但要避免冲突
  • 私人声明:在双方都同意使用的基础上进行使用
{
  "sub": "1234567890",
  "name": "John Doe",
  "admin": true
}

这个 JSON 也会被 Base64Url 编码,然后成为 token 的第二部分。

Signature

编码后的 header + . + 编码后的 payload 作为签名对象,使用 header 中指定的算法,和一个密钥,对签名对象进行签名。这个签名用于认证 token 的不可篡改性。

HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
  secret)

签名结果被 Base64Url 编码后,成为 token 的第三部分。

当使用 HMAC 的加密方式时,用于生成 token 和验证 token 的密钥是同一个。因此,token 产生过程和验证过程无法分开。如果分开,那就意味着相同的密钥分布了很多份,一旦泄露,就有灾难性的后果。这与对称加密的性质类似。

而当使用 RSA 或 ECDSA 的加密方式时,生成 token 和验证 token 的过程就可以解耦了。生成 token 的过程可以由一个专门的认证服务器负责。认证服务器持有私钥,负责对 token 的前两部分进行签名;而普通的应用服务器只负责认证,任意多的应用服务器全部持有公钥也不会有什么问题 - 用公钥解密签名后,如果内容未经篡改,就说明一定是认证服务器签发的未经篡改的 token。

这样一来,只有两种可能才会遭受攻击:

  1. 账号密码泄露 - 通过账号密码可以直接获取合法 token
  2. 私钥泄露 - 这样攻击者可以任意伪造 token

How do JWT work?

当用户使用账号密码成功登录后,产生一个 JWT 并返回。之后用户如果想要访问受保护的路由或资源,就需要在请求中加入 JWT。通常来说,token 以 Bearer 模式被放置在 Authorization 头部:

Authorization: Bearer <token>

成为一种无状态的认证机制。如果 token 中已经包含了必要的认证信息,那么查询数据库的操作就可以被省略了。

Compare

类似的东西还有 Simple Web Tokens (SWT) 和 Security Assertion Martup Language Tokens (SAML)。

由于 JSON 比 XML 的冗余性小,因此 JWT 比 SAML 编码后更小,更适合 HTTP 环境下的传输。而 SWT 只能使用对称加密进行签名。


Edit this page on GitHub
Prev
Cryptography - GPG
Next
Cryptography - Keystore & Certificates