vaszloy/yii2-jwt

简化JWT集成的特性

安装: 13

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 0

分支: 20

类型:yii2-extension

v0.2.3 2022-03-16 15:25 UTC

This package is not auto-updated.

Last update: 2024-09-27 01:46:12 UTC


README

为Yii2授权流程实现JWT

详情请见 JWT官方网站.

安装

要安装(目前只有master分支可用),运行

    composer require "vaszloy/yii2-jwt"

或者将此行添加到 composer.json 的 require 部分中

    "vaszloy/yii2-jwt": "v5.0"

使用方法

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

设置

在控制器中

<?php

// ...

use yii\filters\auth\HttpBearerAuth;

class BearerAuthController extends \yii\rest\ActiveController
{
    public function behaviors()
    {
        return array_merge(parent::behaviors(), [
            'bearerAuth' => [
                'class' => 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 \damirka\JWT\UserTrait;

    // Override this method
    protected static function getSecretKey()
    {
        return 'someSecretKey';
    }

    // And this one if you wish
    protected static function getHeaderToken()
    {
        return [];
    }

    // ...
}