# webman-jwt **Repository Path**: yzh52521/webman-jwt ## Basic Information - **Project Name**: webman-jwt - **Description**: webman jwt auth - **Primary Language**: PHP - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2023-10-11 - **Last Updated**: 2023-10-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # webman-jwt ## 安装 ```sh $ composer require xpzhu/webman-jwt ``` ## 使用 1. 配置 `config/plugin/xpzhu/jwt/app.php` 完整配置 ```php true, 'manager' => [ // 缓存前缀 'prefix' => 'xpzhu_jwt', // 黑名单缓存名 'blacklist' => 'blacklist', // 白名单缓存名 'whitelist' => 'whitelist', ], 'stores' => [ // 单应用 'default' => [ 'sso_enable' => false,// false是多点登录,true是单点登录 'signer_key' => 'ga4FGlxSzNbQKdfNwRhIYJ3BAuPpiEWqxAToNHZqJVvMwWOsa0yKlm1g2fnPuDhz', 'public_key' => 'file://path/public.key', 'private_key' => 'file://path/private.key', 'not_before' => 0, 'expires_at' => 3600, 'refresh_ttl' => 7200, 'signer' => 'Lcobucci\JWT\Signer\Hmac\Sha256', 'type' => 'Header', 'relogin_code' => 50001, 'refresh_code' => 50002, 'iss' => 'jwt.client.default', 'aud' => 'jwt.server.default', 'automatic_renewal' => false, ], // 多应用 'admin' => [ 'sso_enable' => false,// false是多点登录,true是单点登录 'signer_key' => 'ga4FGlxSzNbQKdfNwRhIYJ3BAuPpiEWqxAToNHZqJVvMwWOsa0yKlm1g2fnPuDhz', 'not_before' => 0, 'expires_at' => 3600, 'refresh_ttl' => 7200, 'signer' => 'Lcobucci\JWT\Signer\Hmac\Sha256', 'type' => 'Header', 'relogin_code' => 50001, 'refresh_code' => 50002, 'iss' => 'jwt.client.admin', 'aud' => 'jwt.server.admin', 'automatic_renewal' => false, ], ], ]; ``` ## token * `sso_enable` false是多点登录,true是单点登录 * `signer_key` 密钥 * `not_before` 时间前不能使用 默认生成后直接使用 * `expires_at` Token有效期(秒) * `signer` 加密算法 * `type` 获取 Token 途径 * `relogin_code` Token过期抛异常code = 50001 * `refresh_code` Token失效异常code = 50002 * `automatic_renewal` [开启过期自动续签](#过期自动续签) ## manager * `prefix` 缓存前缀 * `blacklist` 黑名单缓存名 * `whitelist` 白名单缓存名 以下两个异常都会抛一个HTTP异常 StatusCode = 401 * `xpzhu\Exception\HasLoggedException` * `xpzhu\Exception\TokenExpiredException` ### 缓存支持 * File * Redis ## Token 生成 ```php namespace app\admin\controller; use xpzhu\Jwt\Facade\JwtAuth; public function login() { //...登录判断逻辑 $uid = 1; // 自动获取当前应用下的jwt配置 return json([ 'token' => JwtAuth::token($uid, ['params1' => 1, 'params2' => 2])->toString(), ]); // 自定义用户模型 return json([ 'token' => JwtAuth::token($uid, ['model' => CustomMember::class])->toString(), ]); } ``` ## Token 验证 自动获取当前应用(多应用下)配置。 ### 手动验证 ```php use xpzhu\Jwt\Facade\JwtAuth; use xpzhu\Jwt\Exception\JwtException; class User { public function test() { try { JwtAuth::verify(); } catch (JwtException $e) { throw new JwtException($e->getMessage(), $e->getCode()); } // 验证成功 } } ``` ### 路由验证 ```php use Webman\Route; use xpzhu\Jwt\JwtAuth; // 自动获取当前应用配置 Route::get('/hello', 'index/index')->middleware(JwtAuth::class); // 自定义应用 使用api应用配置 Route::get('/hello', 'index/index')->middleware(new xpzhu\Jwt\JwtAuth('api')); ``` ## Token 自动获取 支持以下方式自动获取 * `Header` * `Url` * `Cookie` 赋值方式 | 类型 | 途径 | 标识 | | ----- |-----| ----- | | Header | Authorization | Bearer Token | | Url | Request | token | | Cookie | Request | token | ## 过期自动续签 `config/plugin/xpzhu/jwt/app.php` `automaticRenewal => true` 系统检测到 Token 已过期, 会自动续期并返回以下 header 信息。 * Automatic-Renewal-Token * Automatic-Renewal-Token-RefreshAt 前端需要接收最新 Token,下次异步请求时,携带此 Token。 ## 注销应用Token(所有) 注销指定应用下所有用户(强制下线 重新登录) ```php use xpzhu\Jwt\Facade\JwtAuth; $store = 'wechat'; JwtAuth::destroyStoreWhitelist($store); ``` ## 注销应用Token(指定某个) 注销指定应用下的指定用户(强制下线 重新登录) ```php use xpzhu\Jwt\Facade\JwtAuth; $uid = '9527'; $store = 'wechat'; JwtAuth::destroyToken($uid, $store); ```