搜索插件、代码仓与 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

所属分类

源码仓库

使用此插件的项目

所在榜单