Skip to content

Commit

Permalink
更新文档
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Jan 3, 2020
1 parent d11efb2 commit 6953ad0
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions doc/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions doc/components/db/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions doc/components/event/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ Event::on(['e1', 'e2'], function(){
});
```

取消事件所有监听:

```php
Event::off('事件名');
Event::off(['事件名1', '事件名2']);
```

### 自定义事件

```php
Expand Down
152 changes: 152 additions & 0 deletions doc/components/httpserver/jwt.md
Original file line number Diff line number Diff line change
@@ -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
<?php
namespace Imi\JWT\Test\Test;

use Imi\Bean\Annotation\Bean;
use Imi\JWT\Annotation\JWTValidation;

/**
* @Bean("A")
*/
class A
{
/**
* @JWTValidation(tokenParam="token", dataParam="data")
*
* @param \Lcobucci\JWT\Token $token
* @param \stdClass $data
* @return array
*/
public function test($token = null, $data = null)
{
return [$token, $data];
}

}
```

**@JWTValidation**

JWT 验证注解

| 属性名称 | 说明 |
|-|-
| name | JWT 配置名称 |
| tokenParam | Token 对象注入的参数名称 |
| dataParam | 数据注入的参数名称 |

0 comments on commit 6953ad0

Please sign in to comment.