{"id":22678284,"url":"https://github.com/hyperf-ext/jwt","last_synced_at":"2025-04-10T02:27:22.828Z","repository":{"id":40564896,"uuid":"295035538","full_name":"hyperf-ext/jwt","owner":"hyperf-ext","description":"The Hyperf JWT package.","archived":false,"fork":false,"pushed_at":"2023-10-07T04:37:14.000Z","size":86,"stargazers_count":51,"open_issues_count":13,"forks_count":23,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T18:13:51.146Z","etag":null,"topics":["auth","hyperf","jwt","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hyperf-ext.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-12T22:18:44.000Z","updated_at":"2024-12-16T05:44:10.000Z","dependencies_parsed_at":"2024-06-18T18:25:00.350Z","dependency_job_id":"2d51652e-36cb-4fa0-b615-00bf9dee82d2","html_url":"https://github.com/hyperf-ext/jwt","commit_stats":{"total_commits":12,"total_committers":4,"mean_commits":3.0,"dds":0.25,"last_synced_commit":"ef9d8938e4a1838632202a33b0f4e2ab01146769"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf-ext%2Fjwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf-ext%2Fjwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf-ext%2Fjwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf-ext%2Fjwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hyperf-ext","download_url":"https://codeload.github.com/hyperf-ext/jwt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248143828,"owners_count":21054835,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auth","hyperf","jwt","php"],"created_at":"2024-12-09T18:14:40.597Z","updated_at":"2025-04-10T02:27:22.808Z","avatar_url":"https://github.com/hyperf-ext.png","language":"PHP","readme":"# Hyperf JWT 组件\n\n该组件基于 [`tymon/jwt-auth`](https://github.com/tymondesigns/jwt-auth )，实现了完整用于 JWT 认证的能力。\n\n该组件并不直接提供身份认证的能力，你可以基于该组件提供的功能特性来实现自己的身份认证。\n\n如果你不想自己动手，可以同时安装 [`hyperf-ext/auth`](https://github.com/hyperf-ext/auth) 组件来获得接近开箱即用的身份认证和授权功能。 \n\n## 安装\n\n```shell script\ncomposer require hyperf-ext/jwt\n```\n\n## 发布配置\n\n```shell script\nphp bin/hyperf.php vendor:publish hyperf-ext/jwt\n```\n\n\u003e 文件位于 `config/autoload/jwt.php`。\n\n## 配置\n\n```php\n[\n    /*\n    |--------------------------------------------------------------------------\n    | JWT 密钥\n    |--------------------------------------------------------------------------\n    |\n    | 该密钥用于签名你的令牌，切记要在 .env 文件中设置。组件提供了一个辅助命令来完成\n    | 这步操作：\n    | `php bin/hyperf.php gen:jwt-secret`\n    |\n    | 注意：该密钥仅用于对称算法（HMAC），RSA 和 ECDSA 使用公私钥体系（见下方）。\n    |\n    | 注意：该值必须使用 BASE64 编码。\n    |\n    */\n\n    'secret' =\u003e env('JWT_SECRET'),\n\n    /*\n    |--------------------------------------------------------------------------\n    | JWT 公私钥\n    |--------------------------------------------------------------------------\n    |\n    | 你使用的算法将决定你的令牌是使用随机字符串（在 `JWT_SECRET` 中定设置）还是\n    | 使用以下公钥和私钥来签名。组件提供了一个辅助命令来完成这步操作：\n    | `php bin/hyperf.php gen:jwt-keypair`\n    |\n    | 对称算法：\n    | HS256、HS384 和 HS512 使用 `JWT_SECRET`。\n    |\n    | 非对称算法：\n    | RS256、RS384 和 RS512 / ES256、ES384 和 ES512 使用下面的公私钥。\n    |\n    */\n\n    'keys' =\u003e [\n        /*\n        |--------------------------------------------------------------------------\n        | 公钥\n        |--------------------------------------------------------------------------\n        |\n        | 你的公钥内容。\n        |\n        */\n\n        'public' =\u003e env('JWT_PUBLIC_KEY'),\n\n        /*\n        |--------------------------------------------------------------------------\n        | 私钥\n        |--------------------------------------------------------------------------\n        |\n        | 你的私钥内容。\n        |\n        */\n\n        'private' =\u003e env('JWT_PRIVATE_KEY'),\n\n        /*\n        |--------------------------------------------------------------------------\n        | 密码\n        |--------------------------------------------------------------------------\n        |\n        | 你的私钥的密码。不需要密码可设置为 `null`。\n        |\n        | 注意：该值必须使用 BASE64 编码。\n        |\n        */\n\n        'passphrase' =\u003e env('JWT_PASSPHRASE'),\n    ],\n\n    /*\n    |--------------------------------------------------------------------------\n    | JWT 生存时间\n    |--------------------------------------------------------------------------\n    |\n    | 指定令牌有效的时长（以秒为单位）。默认为 1 小时。\n    |\n    | 你可以将其设置为 `null`，以产生永不过期的令牌。某些场景下有人可能想要这种行为，\n    | 例如在用于手机应用的情况下。\n    | 不太推荐这样做，因此请确保你有适当的体系来在必要时可以撤消令牌。\n    | 注意：如果将其设置为 `null`，则应从 `required_claims` 列表中删除 `exp` 元素。\n    |\n    */\n\n    'ttl' =\u003e env('JWT_TTL', 3600),\n\n    /*\n    |--------------------------------------------------------------------------\n    | 刷新生存时间\n    |--------------------------------------------------------------------------\n    |\n    | 指定一个时长以在其有效期内可刷新令牌（以秒为单位）。 例如，用户可以\n    | 在创建原始令牌后的 2 周内刷新该令牌，直到他们必须重新进行身份验证为止。\n    | 默认为 2 周。\n    |\n    | 你可以将其设置为 `null`，以提供无限的刷新时间。某些场景下有人可能想要这种行为，\n    | 而不是永不过期的令牌，例如在用于手机应用的情况下。\n    | 不太推荐这样做，因此请确保你有适当的体系来在必要时可以撤消令牌。\n    |\n    */\n\n    'refresh_ttl' =\u003e env('JWT_REFRESH_TTL', 3600 * 24 * 14),\n\n    /*\n    |--------------------------------------------------------------------------\n    | JWT 哈希算法\n    |--------------------------------------------------------------------------\n    |\n    | 用于签名你的令牌的哈希算法。\n    |\n    | 关于算法的详细描述可参阅 https://tools.ietf.org/html/rfc7518。\n    |\n    | 可能的值：HS256, HS384, HS512, RS256, RS384, RS512, ES256, ES384, ES512\n    |\n    */\n\n    'algo' =\u003e env('JWT_ALGO', 'HS512'),\n\n    /*\n    |--------------------------------------------------------------------------\n    | 必要声明\n    |--------------------------------------------------------------------------\n    |\n    | 指定在任一令牌中必须存在的必要声明。如果在有效载荷中不存在这些声明中的任意一个，\n    | 则将抛出 `TokenInvalidException` 异常。\n    |\n    */\n\n    'required_claims' =\u003e [\n        'iss',\n        'iat',\n        'exp',\n        'nbf',\n        'sub',\n        'jti',\n    ],\n\n    /*\n    |--------------------------------------------------------------------------\n    | 保留声明\n    |--------------------------------------------------------------------------\n    |\n    | 指定在刷新令牌时要保留的声明的键名。\n    | 除了这些声明之外，`sub`、`iat` 和 `prv`（如果有）声明也将自动保留。\n    |\n    | 注意：如果有声明不存在，则会将其忽略。\n    |\n    */\n\n    'persistent_claims' =\u003e [\n        // 'foo',\n        // 'bar',\n    ],\n\n    /*\n    |--------------------------------------------------------------------------\n    | 锁定主题声明\n    |--------------------------------------------------------------------------\n    |\n    | 这将决定是否将一个 `prv` 声明自动添加到令牌中。\n    | 此目的是确保在你拥有多个身份验证模型时，例如 `App\\User` 和 `App\\OtherPerson`，\n    | 如果两个令牌在两个不同的模型中碰巧具有相同的 ID（`sub` 声明），则我们应当防止\n    | 一个身份验证请求冒充另一个身份验证请求。\n    |\n    | 在特定情况下，你可能需要禁用该行为，例如你只有一个身份验证模型的情况下，\n    | 这可以减少一些令牌大小。\n    |\n    */\n\n    'lock_subject' =\u003e true,\n\n    /*\n    |--------------------------------------------------------------------------\n    | 时间容差\n    |--------------------------------------------------------------------------\n    |\n    | 该属性为 JWT 的时间戳类声明提供了一些时间上的容差。\n    | 这意味着，如果你的某些服务器上不可避免地存在轻微的时钟偏差，\n    | 那么这将可以为此提供一定程度的缓冲。\n    |\n    | 该设置适用于 `iat`、`nbf` 和 `exp`声明。\n    | 以秒为单位设置该值，仅在你了解你真正需要它时才指定。\n    |\n    */\n\n    'leeway' =\u003e env('JWT_LEEWAY', 0),\n\n    /*\n    |--------------------------------------------------------------------------\n    | 启用黑名单\n    |--------------------------------------------------------------------------\n    |\n    | 为使令牌无效，你必须启用黑名单。\n    | 如果你不想或不需要此功能，请将其设置为 `false`。\n    |\n    */\n\n    'blacklist_enabled' =\u003e env('JWT_BLACKLIST_ENABLED', true),\n\n    /*\n    | -------------------------------------------------------------------------\n    | 黑名单宽限期\n    | -------------------------------------------------------------------------\n    |\n    | 当使用同一个 JWT 发送多个并发请求时，由于每次请求都会重新生成令牌，\n    | 因此其中一些可能会失败。\n    |\n    | 设置宽限期（以秒为单位）以防止并发请求失败。\n    |\n    */\n\n    'blacklist_grace_period' =\u003e env('JWT_BLACKLIST_GRACE_PERIOD', 0),\n\n    /*\n    |--------------------------------------------------------------------------\n    | 黑名单存储\n    |--------------------------------------------------------------------------\n    |\n    | 指定用于实现在黑名单中存储令牌行为的类。\n    |\n    | 自定义存储类需要实现 `HyperfExt\\Jwt\\Contracts\\StorageInterface` 接口。\n    |\n    */\n\n    'blacklist_storage' =\u003e HyperfExt\\Jwt\\Storage\\HyperfCache::class,\n];\n```\n\n## 使用\n\n如果你使用 [`hyperf-ext/auth`](https://github.com/hyperf-ext/auth) 组件，则可以忽略该部分。\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nuse HyperfExt\\Jwt\\Contracts\\JwtFactoryInterface;\nuse HyperfExt\\Jwt\\Contracts\\ManagerInterface;\n\nclass SomeClass\n{\n    /**\n     * 提供了对 JWT 编解码、刷新和失活的能力。\n     *\n     * @var \\HyperfExt\\Jwt\\Contracts\\ManagerInterface\n     */\n    protected $manager;\n\n    /**\n     * 提供了从请求解析 JWT 及对 JWT 进行一系列相关操作的能力。\n     *\n     * @var \\HyperfExt\\Jwt\\Jwt\n     */\n    protected $jwt;\n\n    public function __construct(\n        ManagerInterface $manager,\n        JwtFactoryInterface $jwtFactory\n    ) {\n        $this-\u003emanager = $manager;\n        $this-\u003ejwt = $jwtFactory-\u003emake();\n    }\n}\n```\n\n可阅读上述两个类来详细了解如何使用。\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperf-ext%2Fjwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhyperf-ext%2Fjwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperf-ext%2Fjwt/lists"}