laravel中对JWT的配置和使用

64 阅读1分钟

JWT官网Home - jwt-auth

  1. config下的auth中配置jwt:

image.png

同时配置provider:

image.png JWTUser的配置:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Notifications\Notifiable;

use Tymon\JWTAuth\Contracts\JWTSubject;

use Illuminate\Foundation\Auth\User as Authenticatable;

class JwtUser extends Authenticatable implements JWTSubject{

use Notifiable;

protected $table = "users";

protected $primary = "id";

protected $fillable = [

'name', 'email', 'password',

];

protected $hidden = [

'password', 'remember_token',

];

/**

     * Get the identifier that will be stored in the subject claim of the JWT.

     *

     * @return mixed

     */

public function getJWTIdentifier(){

return $this->getKey();

}

/**

     * Return a key value array, containing any custom claims to be added to the JWT.

     *

     * @return array

     */

public function getJWTCustomClaims()

{

return [];

}

}

最后在控制器中使用时guard要指定api:

image.png