Use JWT 具有 RSA signatures (RS256)
RS256 signs JWTs 具有 RSA-SHA256, providing asymmetric 验证 where 仅私有键 holder 能够 sign 但任何人具有公共键 能够验证.
- 1
JWT supports 多个签名 algorithms. RS256 (RSA-SHA256) 是 asymmetric: 授权服务器 signs 具有私有键, 和任何服务能够验证具有公共键. 此是 preferable 到 HS256 (HMAC) 在 distributed systems where sharing symmetric 键是 impractical.
- 2 JWT 编码器 →
Create JWT 具有 RS256. Set 头 alg 到 "RS256", 包含 claims (sub, exp, iat, iss, aud), 和 sign 具有 RSA 私有键. 公共键 是 distributed via JWKS (JSON Web 键 Set).
- 3 JWT 解码器 →
解码 and verify the JWT. Verify the RS256 signature using the public key from the JWKS endpoint. Check claims: exp (not expired), aud (correct audience), iss (trusted issuer). Never skip signature verification.
- 4
- 5
键管理: 发布公共键 在 well-known JWKS URL (e.g., /.well-known/jwks.json). Rotate 签名键 periodically — 包含 kid (键 ID) 在 JWT 头所以 verifiers 知道哪个键 到 use. Support grace 周期具有两者旧 和新 关键期间 rotation.
常见问题
- RS256 vs HS256 — 哪个到 use?
- Use RS256 当多个 services need 到验证 tokens 无 sharing secret. Use HS256 仅在 单个-服务器或 tightly coupled 系统 where sharing symmetric 键是 可行. RS256 是默认为 OAuth2/OIDC.
- 如何 I 处理键 rotation?
- Use kid (键 ID) 头参数. 期间 rotation, 发布两者旧 和新 关键在 JWKS. Tokens signed 具有旧 键保持有效直到它们 expire. 之后所有旧 tokens expire, 移除旧 键从 JWKS.