laraval框架api token 插件 "tymon/jwt-auth": "1.0.0-rc.1"

首先安装(命令:

composer require tymon/jwt-auth 1.0.0-rc.1

  )

也可以在composer.json文件中的require上写(

"tymon/jwt-auth": "1.0.0-rc.1"

接着运行composer update命令

下面就开始配置

在config目录下的app.php文件    prov数组中添加(

Tymon\JWTAuth\Providers\LaravelServiceProvider::class,

接着运行如下命令(

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"

此命令会在 config 目录下生成一个 jwt.php 配置文件,你可以在此进行自定义配置。

接下来运行命令(

php artisan jwt:secret

此命令会在你的 .env 文件中新增一行 JWT_SECRET=secret

接下来在 config/auth.php 文件中,你需要将 guards/driver 更新为 jwt

model写法

<?php
namespace App;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements JWTSubject
{
    use Notifiable;
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }
    public function getJWTCustomClaims()
    {
        return [];
    }
}

接着可以自己定义中间件   或者在控制器中写

猜你喜欢

转载自blog.csdn.net/servicesYY/article/details/82253091