chinaphp/yii2-jwt

用于简化JWT集成的特性

安装: 20

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 0

分支: 20

类型:yii2-extension

v1.0.2 2023-02-24 15:49 UTC

This package is auto-updated.

Last update: 2024-09-26 07:24:06 UTC


README

https://github.com/damirka/yii2-jwt 分支

yii2-jwt

为Yii2授权过程实现的JWT实现

详细信息请参阅 JWT官方网站.

安装

要安装(目前只有master版本可用),运行

    composer require "chinaphp/yii2-jwt:~1.0.0"

或者在composer.json的require部分添加此行

    "chinaphp/yii2-jwt": "~1.0.0"

用法

只有一个特性 - UserTrait - 它为用户模型提供5个用于授权和JWT管理的方 法

项目

您的项目需要是 yii2-app-advanced ,以下是该 指南

设置

在common/config/params.php中

<?php
$params = [
    'JWT_SECRET' => 'your_secret',
    'JWT_EXPIRE' => 10*24*60*60
]

在控制器中

<?php

// ...
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;

class BearerAuthController extends \yii\rest\ActiveController
{
    public function behaviors()
    {
        return array_merge(parent::behaviors(), [
            'authenticator' => [
                'class' => CompositeAuth::className(),
                'authMethods' => [HttpBearerAuth::className(),],
            ]
        ]);
    }
}

在用户模型中

<?php

// ...

use yii\db\ActiveRecord;
use yii\web\IdentityInterface

class User extends ActiveRecord implements IdentityInterface
{
    // Use the trait in your User model
    use \chinaphp\JWT\UserTrait;
}

获取jwt

<?php
// $user is an User object
$token = $user->getJwt()