搜尋套件、儲存庫與 APP 分類

套件詳情v2.0.1

jwt_decoder

這個小型程式庫幫助您解碼 JSON Web 權杖,您也可以知道 JWT 是否已經過期。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
65.5萬
喜歡數
552
Pub 分數
160

安裝套件

flutter pub add jwt_decoder

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

jwt_decoder 套件是一個輕量級的 Dart 庫,專為高效解碼 JSON Web 權杖 (JWT) 而設計。它讓開發者能夠從 JWT 中提取載入資料,並檢查權杖是否已過期。此套件以簡單性為設計核心,支援所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。該套件非常適合需要安全處理權杖的應用程式,例如依賴 JWT 進行使用者會話管理的驗證系統或 API 整合。

適用情境

  • 在身份驗證流程中解析 JWT 負載
  • 在 API 呼叫前驗證權杖過期
  • 從令牌中提取用戶聲明
  • 在跨平台應用中整合基於 JWT 的驗證
  • 開發過程中調試 JWT 結構

主要特色

  • JWT 的簡單解碼
  • 內建過期檢查
  • 跨平台相容性
  • 輕量且依賴極少
  • 支援所有 Flutter 平台

適合對象

  • 使用 JWT 認證的 Flutter 應用
  • 需要快速檢查JWT的開發人員
  • 後端整合JWT令牌
  • 需要安全令牌驗證的應用程式

使用建議

  • 不驗證 JWT 簽名
  • 僅解碼,不驗證真實性
  • 未經額外驗證,不適用於生產環境的安全檢查
  • 需要外部邏輯進行簽名驗證

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

JWT Decoder

This is a small library for decoding a json web token for dart / flutter. Since the header and payload is base64 encoded you can easily know the stored data with no password, you can also know if the token is expired or not.

If you like this library there's a version for Vue here. And for React here

Getting Started

Decode a token
main () {
  String yourToken = "Your JWT";
  Map<String, dynamic> decodedToken = JwtDecoder.decode(yourToken);

  /*
  If the token has a valid format, you will get a Map<String, dynamic>
  Your decoded token can look like:
  {
     "sub": "1234567890",
     "name": "Gustavo",
     "iat": 1516239022,
     "exp": 1516239022,
     "randomKey": "something else"
  }
  */
}
Know if the token is expired
main () {
  String yourToken = "Your JWT";
  bool hasExpired = JwtDecoder.isExpired(yourToken);

  // You will get a true / false response
  // true: if the token is already expired
  // false: if the token is not expired
}
Get expiration date
main () {
  String yourToken = "Your JWT";
  DateTime expirationDate = JwtDecoder.getExpirationDate(token);

  // 2025-01-13 13:04:18.000
  print(expirationDate);
}
You can know how old your token is
// Token payload must include an 'iat' field
main () {
  String yourToken = "Your JWT";
  Duration tokenTime = JwtDecoder.getTokenTime(token);

  // 15
  print(tokenTime.inDays);
}

License

MIT

所屬分類

原始碼儲存庫

使用此套件的專案

所在榜單