msheng/yii2-jwt

简化JWT集成的特性

安装次数: 5,588

依赖项: 0

建议者: 0

安全性: 0

星标: 7

关注者: 2

分支: 20

类型:yii2-extension

1.0.1 2016-08-09 03:17 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:58:28 UTC


README

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

yii2-jwt

Yii2授权过程中的JWT实现

详细信息请查看 JWT官方网站.

安装

要安装(目前只提供master分支)运行

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

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

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

用法

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

在User模型中

<?php

// ...

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

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

获取jwt

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