jwt安装
1 composer require tymon/jwt-auth
jwt配置 1、发布配置文件 1 2 # 这条命令会在 config 下增加一个 jwt.php 的配置文件 php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
2、生成加密密钥 1 2 # 这条命令会在 .env 文件下生成一个加密密钥,如:JWT_SECRET=foobar php artisan jwt:secret
3、更新你的模型 如果你使用默认的 User 表来生成 token,你需要在该模型下增加一段代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 <?php namespace App \Models ;use Illuminate \Contracts \Auth \MustVerifyEmail ;use Illuminate \Database \Eloquent \Factories \HasFactory ;use Illuminate \Foundation \Auth \User as Authenticatable ;use Illuminate \Notifications \Notifiable ;use Laravel \Sanctum \HasApiTokens ;class User extends Authenticatable { use HasApiTokens , HasFactory , Notifiable ; } use Tymon \JWTAuth \Contracts \JWTSubject ;class User extends Authenticatable implements JWTSubject { use HasApiTokens , HasFactory , Notifiable ; public function getJWTIdentifier ( ) { return $this ->getKey (); } public function getJWTCustomClaims ( ) { return []; } }
4、注册两个 Facade 这两个 Facade 并不是必须的,但是使用它们会给你的代码编写带来一点便利。
1 2 3 4 5 6 7 8 9 10 11 <?php return [ ... 'aliases' => [ ... 'JWTAuth' => 'Tymon\JWTAuth\Facades\JWTAuth' , 'JWTFactory' => 'Tymon\JWTAuth\Facades\JWTFactory' , ], ]
5、修改 auth.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <?php return [ 'defaults' => [ 'guard' => 'api' , 'passwords' => 'users' , ], 'guards' => [ 'web' => [ 'driver' => 'session' , 'provider' => 'users' , ], 'api' => [ 'driver' => 'jwt' , 'provider' => 'users' , ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent' , 'model' => \App\Models\User ::class , ], ], ... ]
6、创建 token 控制器 1 php artisan make:controller Api/v1/AuthController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 <?php namespace App \Http \Controllers \Api \v1 ;use App \Http \Controllers \Controller ;use Tymon \JWTAuth \Facades \JWTAuth ;class AuthController extends Controller { public function __construct ( ) { $this ->middleware ('auth:api' , ['except' => ['login' , 'logout' ]]); } public function login ( ) { $credentials = request (['email' , 'password' ]); if (! $token = auth ('api' )->attempt ($credentials )) { return response ()->json (['error' => 'Unauthorized' ], 401 ); } return $this ->respondWithToken ($token ); } protected function respondWithToken ($token ) { return response ()->json ([ 'token' => $token , 'token_type' => 'bearer' , 'expires_in' => auth ('api' )->factory ()->getTTL () * 60 ]); } ... }
7、注册路由
1 2 3 4 5 6 7 8 9 <?php Route ::group ([ 'prefix' => 'auth' ], function ($router ) { Route ::post ('login' , [\App\Http\Controllers\Api\v1\AuthController ::class , 'login' ]); ... });
实现 jwt token 过期时间动态设置(记住我) jwt token 的过期时间在 config/jwt.php 中可以配置,默认1小时。
而实际项目中,前端登陆时有【记住我】这个功能,勾选后需要过期时间设置为7天。最终找到解决办法如下:
App\Http\Controllers\Api\v1\AuthController::login
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public function login ( ) { $credentials = request (['name' , 'password' ]); if (request (['remember' ])) { JWTAuth ::factory ()->setTTL (60 * 24 * 7 ); } $token = JWTAuth ::attempt ($credentials ); if (! $token ) { return response ()->json (['error' => 'Unauthorized' ], 401 ); } return $this ->respondWithToken ($token ); }
软件版本:laravel 8.54 参考资料