为什么客户端凭据应与 Laravel Passport 中的用户相关联?
Why should Client Creadentials be associated with a user in Laravel Passport?
我想使用客户端凭据对客户端应用程序进行身份验证以访问 API。
我的问题是创建客户端凭据。使用 php artisan passport:client
要求我输入 user_id 以将客户端与该用户相关联。我不明白。为什么客户端应用程序必须与用户相关联?!或者还有别的办法吗?
passport:client 命令只支持创建Password Grant Clients 和Personal Grant Client。我认为他们中的任何一个都不是我需要的。
我真正需要的是创建客户端凭据,客户端应用程序仅使用该凭据来授权自己访问某些 APIs。怎么做?
我假设您想使用机器对机器的身份验证(无用户交互)
我建议通读几遍文档以掌握它的窍门。
我不相信有创建唯一客户端凭据客户端的特定方法,我所做的是创建一个个人客户端然后更改数据库中个人客户端的字段personal_access_client
1
=> 0
您可以使用个人客户端选项,如 --help
选项所示
Usage:
passport:client [options]
Options:
--personal Create a personal access token client
--password Create a password grant client
--name[=NAME] The name of the client
-h, --help Display this help message
...
php artisan passport:client --personal
输出
Personal access client created successfully.
Client ID: 1
Client Secret: LbjQNxK5SQZ3pPrEBUwbkE8vaRkg8jh25Qh43HYy
您需要使用默认中间件以外的其他中间件,因为使用此方法时没有用户在场
- 在内核中定义客户端凭证别名中间件
- 将中间件添加到路由
- 发送请求
为 http 内核定义客户端凭证中间件
Class\App\Http\Kernel
:
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'client_credentials' => \Laravel\Passport\Http\Middleware\CheckClientCredentials::class,
//ommited
];
在路由上定义中间件
Route::get('/test', 'ApiTestController@test')->middleware('client_credentials');
Class \App\Http\Controllers\ApiTestController
:
public function test() {
return response()->json(['data' => 'hey'] );
}
来自 php artisan route:list
GET|HEAD | api/test | App\Http\Controllers\ApiTestController@test | api,client_credentials |
发送请求
遵循 client-credentials-grant-tokens 文档中的指定请求
为了简单起见,我使用 Postman,用 Postman 轻松发送测试请求(www.getpostman.com)
将授权设置为 OAuth 2.0,图片:Postman authentication
将访问令牌 URL、客户端 ID、客户端密码和授权类型设置为 'Client Credentials',图像:Postman OAuth Fields
Postman 创建一个令牌并将其附加到 URL 或 Header,在本例中为 header
Accept:application/json
Authorization:Bearer eyJ0eXAiOi...KCjK0
回复:
{
"data": "hey"
}
我正要发表评论,但我的声望还不够=p
您可以为命令提供一个不需要任何用户输入的名称参数。至于如何在没有人工干预的情况下将客户机密交给客户,这才是真正的魔法所在。
php artisan passport:client --personal --name={someName}</pre>
该命令仍然会给你:
Personal access client created successfully.
Client ID: 1
Client Secret: LbjQNxK5SQZ3pPrEBUwbkE8vaRkg8jh25Qh43HYy
</pre>
符合预期。
这些答案有点老了。
您当然可以添加客户端凭据。
php artisan passport:client --client
protected $signature = 'passport:client
{--personal : Create a personal access token client}
{--password : Create a password grant client}
{--client : Create a client credentials grant client}
{--name= : The name of the client}
{--provider= : The name of the user provider}
{--redirect_uri= : The URI to redirect to after authorization }
{--user_id= : The user ID the client should be assigned to }
{--public : Create a public client (Auth code grant type only) }';
我想使用客户端凭据对客户端应用程序进行身份验证以访问 API。
我的问题是创建客户端凭据。使用 php artisan passport:client
要求我输入 user_id 以将客户端与该用户相关联。我不明白。为什么客户端应用程序必须与用户相关联?!或者还有别的办法吗?
passport:client 命令只支持创建Password Grant Clients 和Personal Grant Client。我认为他们中的任何一个都不是我需要的。
我真正需要的是创建客户端凭据,客户端应用程序仅使用该凭据来授权自己访问某些 APIs。怎么做?
我假设您想使用机器对机器的身份验证(无用户交互)
我建议通读几遍文档以掌握它的窍门。
我不相信有创建唯一客户端凭据客户端的特定方法,我所做的是创建一个个人客户端然后更改数据库中个人客户端的字段personal_access_client
1
=> 0
您可以使用个人客户端选项,如 --help
选项所示
Usage:
passport:client [options]
Options:
--personal Create a personal access token client
--password Create a password grant client
--name[=NAME] The name of the client
-h, --help Display this help message
...
php artisan passport:client --personal
输出
Personal access client created successfully.
Client ID: 1
Client Secret: LbjQNxK5SQZ3pPrEBUwbkE8vaRkg8jh25Qh43HYy
您需要使用默认中间件以外的其他中间件,因为使用此方法时没有用户在场
- 在内核中定义客户端凭证别名中间件
- 将中间件添加到路由
- 发送请求
为 http 内核定义客户端凭证中间件
Class\App\Http\Kernel
:
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'client_credentials' => \Laravel\Passport\Http\Middleware\CheckClientCredentials::class,
//ommited
];
在路由上定义中间件
Route::get('/test', 'ApiTestController@test')->middleware('client_credentials');
Class \App\Http\Controllers\ApiTestController
:
public function test() {
return response()->json(['data' => 'hey'] );
}
来自 php artisan route:list
GET|HEAD | api/test | App\Http\Controllers\ApiTestController@test | api,client_credentials |
发送请求
遵循 client-credentials-grant-tokens 文档中的指定请求
为了简单起见,我使用 Postman,用 Postman 轻松发送测试请求(www.getpostman.com)
将授权设置为 OAuth 2.0,图片:Postman authentication
将访问令牌 URL、客户端 ID、客户端密码和授权类型设置为 'Client Credentials',图像:Postman OAuth Fields
Postman 创建一个令牌并将其附加到 URL 或 Header,在本例中为 header
Accept:application/json
Authorization:Bearer eyJ0eXAiOi...KCjK0
回复:
{
"data": "hey"
}
我正要发表评论,但我的声望还不够=p
您可以为命令提供一个不需要任何用户输入的名称参数。至于如何在没有人工干预的情况下将客户机密交给客户,这才是真正的魔法所在。
php artisan passport:client --personal --name={someName}</pre>
该命令仍然会给你:
Personal access client created successfully. Client ID: 1 Client Secret: LbjQNxK5SQZ3pPrEBUwbkE8vaRkg8jh25Qh43HYy </pre>
符合预期。
这些答案有点老了。
您当然可以添加客户端凭据。
php artisan passport:client --client
protected $signature = 'passport:client
{--personal : Create a personal access token client}
{--password : Create a password grant client}
{--client : Create a client credentials grant client}
{--name= : The name of the client}
{--provider= : The name of the user provider}
{--redirect_uri= : The URI to redirect to after authorization }
{--user_id= : The user ID the client should be assigned to }
{--public : Create a public client (Auth code grant type only) }';