1 Star 2 Fork 2

imi 开发组 / imi-jwt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

imi-jwt

Latest Version Php Version Swoole Version IMI License

介绍

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

Composer

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

{
    "require": {
        "imiphp/imi-jwt": "~1.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

简单生成:

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

手动验证:

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
{
    // 不合法
}

注解验证:

<?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 配置名称
id 验证 ID。为 null 则使用配置中的值验证;为 false 则不验证
issuer 验证发行人。为 null 则使用配置中的值验证;为 false 则不验证
audience 验证接收。为 null 则使用配置中的值验证;为 false 则不验证
subject 验证主题。为 null 则使用配置中的值验证;为 false 则不验证
tokenParam Token 对象注入的参数名称
dataParam 数据注入的参数名称

免费技术支持

QQ群:17916227 点击加群,如有问题会有人解答和修复。

运行环境

版权信息

imi-jwt 遵循 MIT 开源协议发布,并提供免费使用。

捐赠

开源不求盈利,多少都是心意,生活不易,随缘随缘……

MIT License Copyright (c) 2018 imiphp Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

在 imi 框架中非常方便地接入 jwt 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/imiphp/imi-jwt.git
git@gitee.com:imiphp/imi-jwt.git
imiphp
imi-jwt
imi-jwt
master

搜索帮助