From 6953ad09a093722dea59c6757b359f8e5b1d9599 Mon Sep 17 00:00:00 2001 From: Yurun Date: Fri, 3 Jan 2020 14:24:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + doc/SUMMARY.md | 1 + doc/components/db/index.md | 24 +++++ doc/components/event/index.md | 7 ++ doc/components/httpserver/jwt.md | 152 +++++++++++++++++++++++++++++++ 5 files changed, 185 insertions(+) create mode 100644 doc/components/httpserver/jwt.md diff --git a/README.md b/README.md index d1580d8d17..864589a061 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ imi 框架交流群:17916227 [![点击加群](https://pub.idqqimg.com/wpa/imag * [Hprose](https://github.com/imiphp/imi-hprose) * [gRPC](https://github.com/imiphp/imi-grpc) * [AMQP](https://github.com/imiphp/imi-amqp) (支持 AMQP 协议的消息队列都可用,如:RabbitMQ) +* [JWT](https://github.com/imiphp/imi-jwt) (在 imi 框架中非常方便地接入 jwt) * [权限控制](https://github.com/imiphp/imi-access-control) * [Smarty 模版引擎](https://github.com/imiphp/imi-smarty) * [限流](https://github.com/imiphp/imi-rate-limit) diff --git a/doc/SUMMARY.md b/doc/SUMMARY.md index f82808f0a2..e6b8539cc8 100644 --- a/doc/SUMMARY.md +++ b/doc/SUMMARY.md @@ -25,6 +25,7 @@ * [控制器](components/httpserver/controller.md) * [RESTful](components/httpserver/restful.md) * [Session](components/httpserver/session.md) +* [JWT](components/httpserver/jwt.md) * [视图](components/httpserver/view.md) * [错误异常处理](components/httpserver/error.md) * [404处理](components/httpserver/404.md) diff --git a/doc/components/db/index.md b/doc/components/db/index.md index 2e447ffedf..377f69a6c8 100644 --- a/doc/components/db/index.md +++ b/doc/components/db/index.md @@ -241,6 +241,30 @@ Db::trans($db, function(IDb $db){ `@Transaction(rollbackType=RollbackType::PART, rollbackLevels="回滚层数,默认为1")` +**事务监听** + +监听提交事务: + +```php +$db = Db::getInstance(); +$db->getTransaction()->onTransactionCommit(function($param){ + $data = $param->getData(); + $db = $data['db']; + $level = $data['level']; // 第几层事务,支持事务嵌套 +}); +``` + +监听回滚事务: + +```php +$db = Db::getInstance(); +$db->getTransaction()->onTransactionRollback(function($param){ + $data = $param->getData(); + $db = $data['db']; + $level = $data['level']; // 第几层事务,支持事务嵌套 +}); +``` + ### 指定表 (table/from) ```php diff --git a/doc/components/event/index.md b/doc/components/event/index.md index 9f35594493..b02df11522 100644 --- a/doc/components/event/index.md +++ b/doc/components/event/index.md @@ -73,6 +73,13 @@ Event::on(['e1', 'e2'], function(){ }); ``` +取消事件所有监听: + +```php +Event::off('事件名'); +Event::off(['事件名1', '事件名2']); +``` + ### 自定义事件 ```php diff --git a/doc/components/httpserver/jwt.md b/doc/components/httpserver/jwt.md new file mode 100644 index 0000000000..8440f6886b --- /dev/null +++ b/doc/components/httpserver/jwt.md @@ -0,0 +1,152 @@ +# JWT + +## 介绍 + +在 imi 框架中非常方便地接入 jwt + +## Composer + +本项目可以使用composer安装,遵循psr-4自动加载规则,在你的 `composer.json` 中加入下面的内容: + +```json +{ + "require": { + "imiphp/imi-jwt": "~1.0" + } +} +``` + +然后执行 `composer update` 安装。 + +## 使用 + +在项目 `config/config.php` 中配置: + +```php +[ + 'components' => [ + // 引入本组件 + 'jwt' => 'Imi\JWT', + ], +] +``` + +### 配置 + +配置 `@app.beans`: + +```php +[ + 'JWT' => [ + 'list' => [ + // a 为名称,可以自定义,以下被注释的项为非必设,一般有默认值 + 'a' => [ + // 'signer' => 'Hmac', // 签名者,可选:Ecdsa/Hmac/Rsa + // 'algo' => 'Sha256', // 算法,可选:Sha256/Sha384/Sha512 + // 'dataName' => 'data', // 自定义数据字段名,放你需要往token里丢的数据 + // 'audience' => null, // 接收,非必须 + // 'subject' => null, // 主题,非必须 + // 'expires' => null, // 超时秒数,非必须 + // 'issuer' => null, // 发行人,非必须 + // 'notBefore' => null, // 实际日期必须大于等于本值 + // 'issuedAt' => true, // JWT 发出时间。设为 true 则为当前时间;设为 false 不设置;其它值则直接写入 + // 'id' => null, // Token id + // 'headers' => [], // 头 + // 自定义获取 token 回调,返回值为 Token。默认从 Header Authorization 中获取。 + // 'tokenHandler' => null, + 'privateKey' => '123456',// 私钥 + 'publicKey' => '123456',// 公钥 + ], + ], + ], +] +``` + +### 生成 Token + +简单生成: + +```php +use \Imi\JWT\Facade\JWT; +// 你需要往token里丢的数据 +$data = [ + 'memberId' => 19260817, +]; +$token = JWT::getToken($data); +``` + +指定名称: + +```php +use \Imi\JWT\Facade\JWT; +// 你需要往token里丢的数据 +$data = [ + 'memberId' => 19260817, +]; +$token = JWT::getToken($data, 'a'); +``` + +自定义处理: + +```php +use \Imi\JWT\Facade\JWT; +// 你需要往token里丢的数据 +$data = [ + 'memberId' => 19260817, +]; +$token = JWT::getToken($data, 'a', function(\Lcobucci\JWT\Builder $builder){ + // 可以针对该对象做一些操作 + $builder->withClaim('aaa', 'bbb'); +}); +``` + +### 验证 Token + +手动验证: + +```php +use \Imi\JWT\Facade\JWT; +/** @var \Lcobucci\JWT\Token $token */ +$token = JWT::parseToken($jwt); +// $token = JWT::parseToken($jwt, 'a'); // 指定配置名称 +$data = $token->getClaim('data'); // 获取往token里丢的数据 +``` + +注解验证: + +```php +