Php 定制Laravel Passport,以便与自己的车型配合使用+;JWT
我希望将JWT与Laravel Passport一起用于我自己的API,但使用我自己的表/模型。到目前为止,我发现的信息仅详细说明了默认安装/配置,或者不完整 我不熟悉Passport,但通过在app/Providers/AppServiceProvider.php中添加“Passport::IgnoreMigrations()”已找到有关如何跳过Passport迁移的信息 我还找到了关于“findForPassport”和“validateForPassportPasswordGrant”的信息,在这里我可以覆盖它获取用户名/密码的位置,但是没有完整的示例可以在任何地方找到,只有零碎的部分 我总是可以选择jwt auth,但无论在哪里,我都会发现人们告诉我护照是官方的通行证,我认为使用护照更能证明未来 互联网上有很多关于如何设置Passport的例子,看起来都很简单,但是如果我不想使用默认的表/模型,那么就很难找到信息。 我的API在没有身份验证的情况下工作,我已经完成了添加Passport身份验证的第一部分:Php 定制Laravel Passport,以便与自己的车型配合使用+;JWT,php,laravel,laravel-passport,Php,Laravel,Laravel Passport,我希望将JWT与Laravel Passport一起用于我自己的API,但使用我自己的表/模型。到目前为止,我发现的信息仅详细说明了默认安装/配置,或者不完整 我不熟悉Passport,但通过在app/Providers/AppServiceProvider.php中添加“Passport::IgnoreMigrations()”已找到有关如何跳过Passport迁移的信息 我还找到了关于“findForPassport”和“validateForPassportPasswordGrant”的信
composer require laravel/passport
Adding the proper service provider
跳过php artisan passport:安装,否则它将尝试向不存在的表添加数据,因此我只生成密钥:
php artisan passport:keys
在my routes/api.php中,我已将所有路由添加到中间件:
Route::middleware('auth:api')->group( function () {
...
}
我还更改了配置/auth.php:
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'User',
],
],
请注意,我的模型是带有表用户的用户。以下是我迁移的一个片段:
Schema::create('users', function (Blueprint $table) {
$table->increments('userid'); /* User ID */
$table->string('username', 30); /* Username */
$table->string('password', 64); /* Password */
$table->string('firstname', 100); /* First name */
$table->string('lastname', 100); /* Last name */
$table->string('email', 128); /* Email address */
$table->binary('avatar')->nullable(true); /* Picture of user */
$table->string('phone',30); /* Phone number */
$table->unsignedInteger('deptid')->nullable(true); /* Department ID */
/* Note that there is a foreign key needed for the department ID but since the order matters in the creation
of the tables I will create a separate migrations file for the foreign keys */
$table->string('timezone', 32); /* Local timezone of the user */
$table->boolean('active'); /* Is the user active (true) / inactive (false) */
$table->dateTime('lastlogon')->nullable(true); /* Last logon date/time */
$table->unsignedInteger('created_by')->nullable(true); /* Which user ID created the record */
$table->unsignedInteger('updated_by')->nullable(true); /* Which user ID updated the record */
$table->timestamps(); /* Timestamps - created_at / updated_at */
});
我将不得不添加令牌和过期日期/时间,但我不知道如何继续
如何覆盖其余部分使其工作?我想要的是返回一个web令牌(当登录成功时),这样就可以用于所有API请求。添加额外的代码以验证web令牌对于每个请求仍然有效,这应该不会有太多工作。
所有这些都不使用Passport使用的默认表。例如,如果您不想使用“用户”模型,并且想使用任何不同的模型进行身份验证。 遵循以下步骤 在config/auth.php中,在grade和provider中玷污该模型
'guards' =>[
......
'customer' => [
'driver' => 'passport',
'provider' => 'customer',
],
]
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'customer' => [
'driver' => 'eloquent',
'model' => App\Customer::class,
],
创建一个新的中间件“CustomerProvider.php”并检查身份验证。在app/http/kernal.php中定义它
'customerapi' => \App\Http\Middleware\PassportCustomProvider::class,
在api.php路径中
Route::middleware('auth:api')->group( function () {
...
}
IMO laravel passport旨在为第三方消费者提供API访问,如果您正在构建一个系统,而您的最终用户不需要创建自己的应用程序并根据您的系统进行身份验证,就像twitter和facebook所做的那样。。。那么使用护照就太过分了。只要使用一些简单的方法,比如
tymon/jwtauth
Ahmad是对的。