搜索插件、代码仓与 APP 分类

插件详情v0.3.1

jwt_decode

一个简单的 Dart 包,用于解码 JSON Web 令牌,并检查其过期时间

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
69.7万
喜欢数
123
Pub 分数
150

安装插件

flutter pub add jwt_decode

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个简单的 Dart 包,用于解码 JSON Web 令牌 (JWT),可轻松访问有效负载数据并验证过期日期。它支持在 Android、iOS、Web、macOS、Windows 和 Linux 等多个平台上解析 JWT。该包非常适合需要安全令牌处理的应用程序,例如身份验证系统或 API 集成。通过 `flutter pub add jwt_decode` 进行最少的设置,开发者可以快速提取声明并验证令牌的有效性。

适合场景

  • 身份验证工作流
  • API令牌验证
  • 用户会话管理
  • 安全数据交换
  • 令牌过期检查

主要特点

  • 轻松解码 JWT
  • 有效期验证
  • 跨平台支持
  • 最小设置
  • 有效负载声明提取

适合对象

  • 移动应用开发者
  • Web 应用程序构建器
  • 后端服务集成商
  • 注重安全的项目
  • 身份验证系统实现者

使用建议

  • 不验证签名
  • 需要外部验证以确保安全
  • 仅限解码,不支持签名
  • 在没有额外检查的情况下不适合用于生产
  • 依赖于正确的令牌格式

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

jwt_decode

jwt_decode is a lightweight library for decoding JSON Web Tokens (JWTs) in Dart.

IMPORTANT: This library does not validate the tokens. It can decode any well-formed ones.

Installation

Add jwt_decode to your project's pubspec.yaml file:

dependencies:
  jwt_decode: ^0.3.1

Then run flutter pub get to install the package.

Using

Here's an example of how to use jwt_decode to decode a JWT:

import 'package:jwt_decode/jwt_decode.dart';

void main() {
  final String token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE1MTYyMzkwMjJ9.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
  final Map<String, dynamic> decodedToken = Jwt.parseJwt(token);
  print(decodedToken);
}

This will output the following:

{
  "sub": "1234567890",
  "name": "John Doe",
  "iat": 1516239022,
  "exp": 1516239022
}

API Reference

Jwt.parseJwt(String token)

Parses the given JWT and returns a map containing the token's claims.

Jwt.getExpiryDate(String token)

Parses the given JWT and returns the DateTime object containing the token's expiry date if the token is valid and null if not.

Jwt.isExpired(String token)

Parses the given JWT and returns true if the token is still valid, and false otherwise.

Contributing

Contributions are welcome! Please see our Contributing Guidelines for more information.

License

jwt_decode is licensed under the BSD 3-clause license.

所属分类

源码仓库

使用此插件的项目