msheng / yii2-jwt
简化JWT集成的特性
1.0.1
2016-08-09 03:17 UTC
Requires
- firebase/php-jwt: ~4.0.0
- yiisoft/yii2: ~2.0.0
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()