laravel 使用dingo 和 jwt 【laravel7.3 dingo3.0 php7.3】配置
运气不错,没有报错,所以用简易方式列表展示我的安装步骤,用来记录。可以略过不看,看下一篇技巧:jwt让管理员和用户使用不同的guard/数据表验证。
JWT官网:https://jwt-auth.readthedocs.io/en/develop/quick-start/
Dingo官网:https://github.com/dingo/api/wiki/Installation
1、安装git,设置环境变量。https://www.cnblogs.com/qingmuchuanqi48/p/12052289.html,如果不安装,composer update时候有可能会出现警告错误。
2、安装dingo,我没有用composer 命令安装,参考官方文档,一定要看第一个表格中版本的说明:https://github.com/dingo/api/wiki/Installation
3、打开 composer.json ,添加:【在require下添加“dingo/api“: “^3.0.0“】即可。
1 2 3 |
<span class="pl-s"><span class="pl-pds">"</span>require<span class="pl-pds">"</span></span>: { <span class="pl-s"><span class="pl-pds">"</span>dingo/api<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>^3.0.0<span class="pl-pds">"</span></span> } |
4、运行 composer update 安装。
5、发布一下配置:
1 |
php artisan vendor:publish --provider="Dingo\Api\Provider\LaravelServiceProvider" |
6、参考dingo 2.0版的中文配置说明:https://learnku.com/docs/dingo-api/2.0.0/Configuration/1444
在根目录下的.env下
1 2 3 4 5 6 7 8 |
API_STANDARDS_TREE=x //分支说明 API_SUBTYPE="${APP_NAME}" //<b>通常是说app的短名称 一般小写</b> API_PREFIX=api //<b>前缀和子域 可以使用域名,比如api.123.com 不要加/</b> API_VERSION=v1 // API_NAME="51SEO API" API_STRICT=false //严格模式 API_DEFAULT_FORMAT=json //<b>响应模式</b> API_DEBUG=false |
7、先不考虑路由配置,先安装jwt:composer require tymon/jwt-auth,官方文档:https://jwt-auth.readthedocs.io/en/develop/
8、发布资源:php artisan vendor:publish –provider=“Tymon\JWTAuth\Providers\LaravelServiceProvider”
9、生成jwt的key: php artisan jwt:secret
10、进行配置
在根目录下的.env下
1 2 3 4 5 |
JWT_SECRET=key值 //php artisan jwt:secret 自动生成的 JWT_TTL=60 #token有效期,默认60分钟,单位分钟,ttl失效,refresh_ttl有效时可以刷新获取新的token JWT_REFRESH_TTL=20160 #token允许刷新时间,默认20160分钟(2周),单位分钟,refresh_ttl失效后,需要重新登陆。 JWT_BLACKLIST_ENABLED=true #黑名单,默认true,建议开启,不开启无法让token失效。 JWT_BLACKLIST_GRACE_PERIOD=10 #宽限时间,单位秒,ttl失效,refresh_ttl有效时宽限时间内的请求会被允许;并发请求时,token被刷新后,防止后续请求失败。 |
11、./config/app.php 配置 ‘providers’项添加:
1 2 |
Tymon\JWTAuth\Providers\LaravelServiceProvider::class, //add jwt服务 Dingo\Api\Provider\LaravelServiceProvider::class, //add api接口服务 |
12、./config/app.php 配置 ‘aliases’项添加:
1 2 3 4 |
'API' => Dingo\Api\Facade\API::class, //dingo api //提供好用的辅助方法 'ApiRoute' => Dingo\Api\Facade\Route::class, //dingo apiRoute //可以获取当前api路由 检查当前路由的名称等等 'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class, //jwt 'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class, //jwt |
13、./config/api.php 修改 ‘auth’项(第170行左右),用于设置dingo的api验证方式:
1 |
'jwt' => 'Dingo\Api\Auth\Provider\JWT', |
14、数据库表【用于用户验证的表】配置
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 |
<?php namespace App\Models; use Encore\Admin\Traits\DefaultDatetimeFormat; use Tymon\JWTAuth\Contracts\JWTSubject; //*这里是新增 use Illuminate\Notifications\Notifiable; //*这里是新增 use Illuminate\Foundation\Auth\User as Authenticatable; //*这里是修改 class AdminUser extends Authenticatable implements JWTSubject //*这里是修改 { //JWT use Notifiable; //*这里是新增 //格式化日期显示 use DefaultDatetimeFormat; //指定表名 因为表名带s protected $table = 'admin_users'; protected $fillable = []; protected $guarded = [ 'remember_token' ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; //实现 JWTSubject接口 新增 两个函数 /** * 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 []; } } |
写完这个,发现网上有一篇图文并茂的配置文章,比我这个写得好,可以点击进去查看。https://blog.csdn.net/cxhblog/article/details/105938036
噢!评论已关闭。