laravel 安装jwt-auth及验证
我的官方群点击此处。
1、使用composer安装jwt,cmd到项目文件夹中;
composer require tymon/jwt-auth 1.0.*(这里版本号根据自己的需要写)
安装jwt ,参考官方文档https://jwt-auth.readthedocs.io/en/docs/laravel-installation/
2、如果laravel版本低于5.4
打开根目录下的config/app.php
在'providers'数组里加上Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
'providers' => [ ... Tymon\JWTAuth\Providers\LaravelServiceProvider::class,]
3、在 config 下增加一个 jwt.php 的配置文件
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
4、在 .env 文件下生成一个加密密钥,如:JWT_SECRET=foobar
php artisan jwt:secret
5、在user模型中写入下列代码
<?php
namespace App\Model;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements JWTSubject
{
// Rest omitted for brevity
protected $table="user";
public $timestamps = false;
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
}
6、注册两个 Facade
config/app.php
'aliases&#