{"id":30506483,"url":"https://github.com/friendsofthinkphp/think-jwt","last_synced_at":"2025-08-25T17:35:19.414Z","repository":{"id":54013849,"uuid":"220160101","full_name":"friendsofthinkphp/think-jwt","owner":"friendsofthinkphp","description":"ThinkPHP Jwt 扩展包","archived":false,"fork":false,"pushed_at":"2021-08-14T09:08:18.000Z","size":216,"stargazers_count":76,"open_issues_count":10,"forks_count":18,"subscribers_count":4,"default_branch":"next","last_synced_at":"2025-07-22T15:54:06.159Z","etag":null,"topics":["jwt","thinkphp","thinkphp-component"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/friendsofthinkphp.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}},"created_at":"2019-11-07T05:41:30.000Z","updated_at":"2024-12-16T07:46:07.000Z","dependencies_parsed_at":"2022-08-13T06:01:01.723Z","dependency_job_id":null,"html_url":"https://github.com/friendsofthinkphp/think-jwt","commit_stats":null,"previous_names":[],"tags_count":50,"template":false,"template_full_name":null,"purl":"pkg:github/friendsofthinkphp/think-jwt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/friendsofthinkphp%2Fthink-jwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/friendsofthinkphp%2Fthink-jwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/friendsofthinkphp%2Fthink-jwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/friendsofthinkphp%2Fthink-jwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/friendsofthinkphp","download_url":"https://codeload.github.com/friendsofthinkphp/think-jwt/tar.gz/refs/heads/next","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/friendsofthinkphp%2Fthink-jwt/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272103222,"owners_count":24874029,"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","status":"online","status_checked_at":"2025-08-25T02:00:12.092Z","response_time":1107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["jwt","thinkphp","thinkphp-component"],"created_at":"2025-08-25T17:35:14.823Z","updated_at":"2025-08-25T17:35:19.404Z","avatar_url":"https://github.com/friendsofthinkphp.png","language":"PHP","readme":"# think-jwt\n\n[![Build Status](https://travis-ci.org/edenleung/think-jwt.svg?branch=master)](https://travis-ci.org/edenleung/think-jwt)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/edenleung/think-jwt/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/edenleung/think-jwt/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/edenleung/think-jwt/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/edenleung/think-jwt/?branch=master)\n\n只支持 `thinkphp 6.0`\n## 安装\n稳定版\n```sh\n$ composer require xiaodi/think-jwt\n```\n\n开发版\n```sh\n$ composer require xiaodi/think-jwt:dev-next\n```\n\n## 使用\n1. 配置\n`config/jwt.php`\n\n完整配置\n```php\n\u003c?php\n\nreturn [\n    'stores' =\u003e [\n        // 单应用时 默认使用此配置\n        'default' =\u003e [\n            'sso' =\u003e [\n                'enable' =\u003e false,\n            ],\n            'token' =\u003e [\n                'signer_key'    =\u003e 'tant',\n                'public_key'    =\u003e 'file://path/public.key',\n                'private_key'   =\u003e 'file://path/private.key',\n                'not_before'    =\u003e 0,\n                'expires_at'    =\u003e 3600,\n                'refresh_ttL'   =\u003e 7200,\n                'signer'       =\u003e 'Lcobucci\\JWT\\Signer\\Hmac\\Sha256',\n                'type'         =\u003e 'Header',\n                'relogin_code'      =\u003e 50001,\n                'refresh_code'      =\u003e 50002,\n                'iss'          =\u003e 'client.tant',\n                'aud'          =\u003e 'server.tant',\n                'automatic_renewal' =\u003e false,\n            ],\n            'user' =\u003e [\n                'bind' =\u003e false,\n                'class'  =\u003e null,\n            ]\n        ],\n        // 多应用时 对应应用的配置\n        'admin' =\u003e [\n            'sso' =\u003e [\n                'enable' =\u003e false,\n            ],\n            'token' =\u003e [\n                'signer_key'    =\u003e 'tant',\n                'not_before'    =\u003e 0,\n                'expires_at'    =\u003e 3600,\n                'refresh_ttL'   =\u003e 7200,\n                'signer'       =\u003e 'Lcobucci\\JWT\\Signer\\Hmac\\Sha256',\n                'type'         =\u003e 'Header',\n                'relogin_code'      =\u003e 50001,\n                'refresh_code'      =\u003e 50002,\n                'iss'          =\u003e 'client.tant',\n                'aud'          =\u003e 'server.tant',\n                'automatic_renewal' =\u003e false,\n            ],\n            'user' =\u003e [\n                'bind' =\u003e false,\n                'class'  =\u003e null,\n            ]\n        ]\n    ],\n    'manager' =\u003e [\n        // 缓存前缀\n        'prefix' =\u003e 'jwt',\n        // 黑名单缓存名\n        'blacklist' =\u003e 'blacklist',\n        // 白名单缓存名\n        'whitelist' =\u003e 'whitelist'\n    ]\n];\n\n```\n## token\n* `signer_key` 密钥\n* `not_before` 时间前不能使用 默认生成后直接使用\n* `refresh_ttL` Token有效期（秒）\n* `signer` 加密算法\n* `type`  获取 Token 途径\n* `relogin_code` Token过期抛异常code = 50001\n* `refresh_code` Token失效异常code = 50002\n* `automatic_renewal` [开启过期自动续签](#过期自动续签)\n\n## user\n* `bind` 是否注入用户模型(中间件有效)\n* `class` 用户模型类文件 \n\n## manager\n* `prefix` 缓存前缀\n* `blacklist` 黑名单缓存名\n* `whitelist` 白名单缓存名\n\n以下两个异常都会抛一个HTTP异常 StatusCode = 401\n* `xiaodi\\Exception\\HasLoggedException`\n* `xiaodi\\Exception\\TokenAlreadyEexpired`\n\n### 缓存支持\n* File\n* Redis\n\n## Token 生成\n```php\nnamespace app\\home\\controller\\Auth;\n\nuse xiaodi\\JWTAuth\\Facade\\Jwt;\n\npublic function login()\n{\n    //...登录判断逻辑\n\n    // 自动获取当前应用下的jwt配置\n    return json([\n        'token' =\u003e Jwt::token($uid, ['params1' =\u003e 1, 'params2' =\u003e 2])-\u003etoString(),\n    ]);\n    \n    // 自定义用户模型\n    return json([\n        'token' =\u003e Jwt::token($uid, ['model' =\u003e CustomMember::class])-\u003etoString(),\n    ]);\n}\n```\n\n## Token 验证\n\n自动获取当前应用（多应用下）配置。\n\n### 手动验证\n```php\nuse xiaodi\\JWTAuth\\Facade\\Jwt;\nuse xiaodi\\JWTAuth\\Exception\\HasLoggedException;\nuse xiaodi\\JWTAuth\\Exception\\TokenAlreadyEexpired;\n\nclass User {\n\n    public function test()\n    {\n        if (true === Jwt::verify($token)) {\n            // 验证成功\n        }\n        \n        // 验证成功\n        // 如配置用户模型文件 可获取当前用户信息\n        dump(Jwt::user());\n    }\n}\n\n```\n\n### 路由验证\n```php\nuse xiaodi\\JWTAuth\\Middleware\\Jwt;\n\n// 自动获取当前应用配置\nRoute::get('/hello', 'index/index')-\u003emiddleware(Jwt::class);\n\n// 自定义应用 使用api应用配置\nRoute::get('/hello', 'index/index')-\u003emiddleware(Jwt::class, 'api');\n```\n\n## Token 自动获取\n\n支持以下方式自动获取\n\n* `Header`\n* `Cookie`\n* `Url`\n\n赋值方式\n\n类型 | 途径 | 标识 |\n:-: | :-: | :-: | \nHeader | Authorization | Bearer Token |\nCookie | Cookie| token |\nUrl | Request | token |\n\n```php\n# config/jwt.php\n\n\u003c?php\n\nreturn [\n\n    'apps' =\u003e [\n        'admin' =\u003e [\n            'token' =\u003e [\n                // ...其它配置\n                'type' =\u003e 'Header',\n                // 'type' =\u003e 'Cookie',\n                // 'type' =\u003e 'Url',\n                // 支持多种方式获取\n                // 'type' =\u003e 'Header|Url',\n            ]\n        ]\n    ]\n    \n];\n```\n\n## 过期自动续签\n`app/config/jwt.php`\n\n`automaticRenewal =\u003e true`\n\n系统检测到 Token 已过期， 会自动续期并返回以下 header 信息。 \n\n* Automatic-Renewal-Token\n* Automatic-Renewal-Token-RefreshAt\n\n前端需要接收最新 Token，下次异步请求时，携带此 Token。\n\n## 注销应用Token(所有)\n\n注销指定应用下缓存的用户 （强制下线 重新登录）\n\n```php\n\n$store = 'wechat';\n\napp('jwt.manager')-\u003edestroyStoreWhitelist($store);\n```\n\n## 注销应用Token(指定某个)\n\n注销指定某个用户（强制下线 重新登录）\n\n```php\n\n$store = 'wechat';\n$uid = '9527';\n\napp('jwt.manager')-\u003edestroyToken($id, $store);\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffriendsofthinkphp%2Fthink-jwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffriendsofthinkphp%2Fthink-jwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffriendsofthinkphp%2Fthink-jwt/lists"}