十分鐘快速入門JWT

JWT 即 JSON Web Token,是為了在網(wǎng)絡(luò)應(yīng)用環(huán)境間傳遞聲明而執(zhí)行的一種基于JSON的開放標(biāo)準(zhǔn)((RFC 7519).該token被設(shè)計為緊湊且安全的,特別適用于分布式站點的單點登錄(SSO)場景。

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA.

JWT應(yīng)用場景

Here are some scenarios where JSON Web Tokens are useful:

  • Authentication: This is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.

  • Information Exchange: JSON Web Tokens are a good way of securely transmitting information between parties, because as they can be signed, for example using public/private key pairs, you can be sure that the senders are who they say they are. Additionally, as the signature is calculated using the header and the payload, you can also verify that the content hasn't been tampered with.

JWT的組成

JSON Web Tokens 由三部分組成:

  • Header(頭部)
  • Payload(載荷)
  • Signature(簽名)

JWT由上述三段信息構(gòu)成的,將這三段信息文本用.鏈接一起就構(gòu)成了JWT字符串,如下:
xxxxx.yyyyy.zzzzz

Header

JWT的頭部由兩部分信息組成:

  • token的類型,這里是JWT
  • 使用的hashing 算法,例如HMAC SHA256 或者 RSA

完整的頭部就像下面這樣的JSON:

{
  "alg": "HS256",
  "typ": "JWT"
}

Payload

載荷就是存放有效信息的地方,這些有效信息包含三個部分:

  • 保留的聲明,例如:iss (issuer), exp (expiration time), sub (subject), aud (audience), and others。
  • 公共的聲明,可以添加任何的信息,一般添加用戶的相關(guān)信息或其他業(yè)務(wù)需要的必要信息,但不建議添加敏感信息,因為該部分在客戶端可解密。
  • 私有的聲明,是提供者和消費者所共同定義的聲明

一個payload示例如下:

{
  "sub": "1234567890",
  "name": "John Doe",
  "admin": true
}

Signature

JWT的第三個部分簽名 需要將base64UrlEncode(header) 和base64UrlEncode(payload)使用.連接組成的字符串,然后通過header中聲明的加密方式進(jìn)行加鹽secret組合加密,原文如下:

To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and > > sign that.

例如,使用HMAC SHA256,Signature計算如下:

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

將這三部分用.連接成一個完整的字符串,構(gòu)成了最終的JWT:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.
TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ

secret是保存在服務(wù)器端的,JWT是由服務(wù)器端生成并返回給客戶端的,secret就是用來進(jìn)行JWT的簽發(fā)和JWT的驗證,所以,它就是你服務(wù)端的私鑰,在任何場景都不應(yīng)該流露出去。

JWT如何工作的

一般是在請求頭里加入Authorization,并加上Bearer標(biāo)注:

Authorization: Bearer <token>

服務(wù)端校驗token的合法性,如果驗證通過就會返回相應(yīng)的資源。整個流程就是這樣的:

jwt-diagram.png

參考資料

https://jwt.io/introduction/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,586評論 19 139
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,660評論 5 6
  • 字體分為襯線字體和非襯線字體 。襯線字體適合做大標(biāo)題。非襯線字體適合正文。 字體第二種分類為系統(tǒng)字體和...
    一然66閱讀 866評論 0 0
  • 今天上午妹妹發(fā)微信給我說:“昨天晚上做夢,夢見媽媽追著打我?!蔽覇査澳阕罱趺蠢献鰤??”妹妹說:“每年到了冬天,...
    YY愛好者閱讀 307評論 0 1
  • 牙痛不是病,痛起來要人命。 這段時間有兩位好友都遇到了牙痛,兩人都因為牙痛而沒有來練瑜伽很多次,聽她們一提牙痛,都...
    惠芝閱讀 575評論 1 1

友情鏈接更多精彩內(nèi)容