damirka/yii2-jwt

简化JWT集成的特性

安装次数: 76,224

依赖项: 0

建议者: 0

安全性: 0

星标: 66

关注者: 4

分支: 20

公开问题: 0

类型:yii2-extension

v0.2.1 2016-08-16 08:29 UTC

This package is not auto-updated.

Last update: 2024-09-16 21:10:00 UTC


README

为Yii2授权流程提供JWT实现

更多详情请参阅 JWT官方网站.

安装

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

    composer require "damirka/yii2-jwt:v0.2.1"

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

    "damirka/yii2-jwt": "v0.2.1"

用法

只有一个特性 - UserTrait - 它为 User 模型提供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()
            ]
        ]);
    }
}

在 User 模型中

<?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 [];
    }

    // ...
}