PHPackages                             imiphp/imi-jwt - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. imiphp/imi-jwt

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

imiphp/imi-jwt
==============

在 imi 框架中非常方便地接入 jwt

v2.1.14(2y ago)39.4k↓50%1MulanPSL-2.0PHP

Since Jan 2Pushed 2y ago1 watchersCompare

[ Source](https://github.com/imiphp/imi-jwt)[ Packagist](https://packagist.org/packages/imiphp/imi-jwt)[ RSS](/packages/imiphp-imi-jwt/feed)WikiDiscussions 2.0 Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (40)Used By (1)

imi-jwt
=======

[](#imi-jwt)

[![Latest Version](https://camo.githubusercontent.com/54154b97a901b774cbf73e4ba1228f78edbf2456e69ef528844d4426bdae5c19/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696d697068702f696d692d6a77742e737667)](https://packagist.org/packages/imiphp/imi-jwt)[![Php Version](https://camo.githubusercontent.com/4a5c2ab20974058a8bab53ecb30ac4c2e6bb961df6229b7386fdc097ab53dfa8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e342d627269676874677265656e2e737667)](https://secure.php.net/)[![Swoole Version](https://camo.githubusercontent.com/f077644cadc3b88104d75a54818d0e1462f910dc6d6dfd9939ea295fb11b4e2b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73776f6f6c652d2533453d342e312e302d627269676874677265656e2e737667)](https://github.com/swoole/swoole-src)[![IMI License](https://camo.githubusercontent.com/5fe9c6ce192b06768e469e5dc949e1e7f5959f0d36c3409513bfc56b8e2958bb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f696d697068702f696d692d6a77742e737667)](https://github.com/imiphp/imi-jwt/blob/master/LICENSE)

介绍
--

[](#介绍)

在 imi 框架中非常方便地接入 jwt

> 本仓库仅用于浏览，不接受 issue 和 Pull Requests，请前往：

Composer
--------

[](#composer)

本项目可以使用composer安装，遵循psr-4自动加载规则，在你的 `composer.json` 中加入下面的内容:

```
{
    "require": {
        "imiphp/imi-jwt": "~2.0.0"
    }
}
```

然后执行 `composer update` 安装。

使用
--

[](#使用)

在项目 `config/config.php` 中配置：

```
[
    'components'    =>  [
        // 引入本组件
        'jwt'    =>  'Imi\JWT',
    ],
]
```

### 配置

[](#配置)

配置 `@app.beans`：

```
[
    '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

[](#生成-token)

简单生成：

```
use \Imi\JWT\Facade\JWT;
// 你需要往token里丢的数据
$data = [
    'memberId'  =>  19260817,
];
$token = JWT::getToken($data); // Token 对象
$tokenContent = $token->__toString(); // Token 字符串
```

指定名称：

```
use \Imi\JWT\Facade\JWT;
// 你需要往token里丢的数据
$data = [
    'memberId'  =>  19260817,
];
$token = JWT::getToken($data, 'a'); // Token 对象
$tokenContent = $token->__toString(); // Token 字符串
```

自定义处理：

```
use \Imi\JWT\Facade\JWT;
// 你需要往token里丢的数据
$data = [
    'memberId'  =>  19260817,
];
$token = JWT::getToken($data, 'a', function(\Lcobucci\JWT\Builder $builder){
    // 可以针对该对象做一些操作
    $builder->withClaim('aaa', 'bbb');
}); // Token 对象
$tokenContent = $token->__toString(); // Token 字符串
```

### 验证 Token

[](#验证-token)

手动验证：

```
use \Imi\JWT\Facade\JWT;
/** @var \Lcobucci\JWT\Token $token */
$token = JWT::parseToken($jwt); // 仅验证是否合法
// $token = JWT::parseToken($jwt, 'a'); // 指定配置名称
$data = $token->getClaim('data'); // 获取往token里丢的数据

// 验证有效期、id、issuer、audience、subject
$validationData = new \Lcobucci\JWT\ValidationData;
$validationData->setId('');
$validationData->setIssuer('');
$validationData->setAudience('');
$validationData->setSubject('');
if($token->validate($validationData))
{
    // 合法
}
else
{
    // 不合法
}
```

注解验证：

```
