xzag/yii3-jwt

用于简化JWT集成的特性

维护者

详细信息

github.com/xzag/yii3-jwt

主页

源代码

安装次数: 6

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 20

类型:yii3-extension

3.0.0 2019-05-06 18:12 UTC

This package is auto-updated.

Last update: 2024-09-07 07:27:52 UTC


README

Yii3的JWT实现

有关详细信息,请参阅 JWT官方网站

安装

要安装,请运行

    composer require "xzag/yii3-jwt: ~3.0"

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

    "xzag/yii3-jwt": "~3.0"

用法

只有一个特性 - UserTrait - 它在 User 模型中提供了5个用于授权和JWT管理的函数

设置

在控制器中

<?php

// ...

use yii\web\filters\auth\HttpBearerAuth;
use Yiisoft\Yii\Rest\ActiveController;

class BearerAuthController extends ActiveController
{
    public function behaviors()
    {
        return array_merge(parent::behaviors(), [
            'bearerAuth' => [
                'class' => HttpBearerAuth::class
            ]
        ]);
    }
}

在User模型中

<?php

// ...

use yii\activerecord\ActiveRecord;
use yii\web\IdentityInterface;

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

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

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

    // ...
}