如何更改令牌守卫中的 api_token 列
how to change api_token column in token guard
Laravel 5.5
我想改变在 TokenGaurd 中使用的 api 令牌的方向,所以,
我创建了一个名为 CafeTokenGaurd extends TokenGuard 的自定义守卫,我在其中定义了 __construct 函数,就像我想要的那样:
public function __construct(UserProvider $provider, Request $request) {
parent::__construct($provider, $request);
$this->inputKey = 'api_key'; // I want changing this part
$this->storageKey = 'api_key';
}
现在我想从与用户的关系 table 中定义 api_key
,如下所示:
device_user table -> token
我想为用户拥有的每个设备定义特定的令牌,我想在用户和设备之间的枢轴 table 中为该列设置 api 键输入和存储键,
我该怎么办?!
谢谢
因为您需要更改从数据库中检索用户的方式,所以您实际上需要创建和使用自定义 UserProvider
,而不是自定义 Guard
。如果您想从 api_token
.
重命名输入键或存储键,则只需要自定义保护
因此,您需要一个新的自定义 UserProvider
class 知道如何使用给定的凭据(令牌)检索您的用户,并且您需要告诉 Auth
使用您的新自定义 UserProvider
class.
首先,假设您仍在使用 Eloquent,首先创建一个扩展基础 EloquentUserProvider
class 的新 UserProvider
class。在此示例中,它创建于 app/Services/Auth/MyEloquentUserProvider.php
。在此 class 中,您需要使用有关如何使用提供的令牌检索用户的详细信息来覆盖 retrieveByCredentials
函数。
namespace App\Services\Auth;
use Illuminate\Auth\EloquentUserProvider;
class MyEloquentUserProvider extends EloquentUserProvider
{
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials)
{
if (empty($credentials)) {
return;
}
// $credentials will be an array that looks like:
// [
// 'api_token' => 'token-value',
// ]
// $this->createModel() will give you a new instance of the class
// defined as the model in the auth config for your application.
// Your logic to find the user with the given token goes here.
// Return found user or null if not found.
}
}
创建 class 后,您需要让 Auth
知道。您可以在 AuthServiceProvider
服务提供商的 boot()
方法中执行此操作。此示例将使用名称 "myeloquent",但您可以使用任何您想要的名称("eloquent" 和 "database" 除外)。
public function boot()
{
$this->registerPolicies();
Auth::provider('myeloquent', function($app, array $config) {
return new \App\Services\Auth\MyEloquentUserProvider($app['hash'], $config['model']);
});
}
最后,您需要告诉 Auth
使用新的 myeloquent
用户提供商。这是在 config/auth.php
配置文件中完成的。
'providers' => [
'users' => [
'driver' => 'myeloquent', // this is the provider name defined above
'model' => App\User::class,
],
],
您可以在 documentation here.
中阅读有关添加自定义用户提供商的更多信息
从版本Laravel 5.7.28开始,您可以在config/auth.php
中简单地设置。
'guards' => [
'api' => [
'driver' => 'token',
'input_key' => 'token', // The input name to pass through
'storage_key' => 'token', // The column name to store in database
'provider' => 'users',
],
],
Laravel 5.5
我想改变在 TokenGaurd 中使用的 api 令牌的方向,所以, 我创建了一个名为 CafeTokenGaurd extends TokenGuard 的自定义守卫,我在其中定义了 __construct 函数,就像我想要的那样:
public function __construct(UserProvider $provider, Request $request) {
parent::__construct($provider, $request);
$this->inputKey = 'api_key'; // I want changing this part
$this->storageKey = 'api_key';
}
现在我想从与用户的关系 table 中定义 api_key
,如下所示:
device_user table -> token
我想为用户拥有的每个设备定义特定的令牌,我想在用户和设备之间的枢轴 table 中为该列设置 api 键输入和存储键,
我该怎么办?!
谢谢
因为您需要更改从数据库中检索用户的方式,所以您实际上需要创建和使用自定义 UserProvider
,而不是自定义 Guard
。如果您想从 api_token
.
因此,您需要一个新的自定义 UserProvider
class 知道如何使用给定的凭据(令牌)检索您的用户,并且您需要告诉 Auth
使用您的新自定义 UserProvider
class.
首先,假设您仍在使用 Eloquent,首先创建一个扩展基础 EloquentUserProvider
class 的新 UserProvider
class。在此示例中,它创建于 app/Services/Auth/MyEloquentUserProvider.php
。在此 class 中,您需要使用有关如何使用提供的令牌检索用户的详细信息来覆盖 retrieveByCredentials
函数。
namespace App\Services\Auth;
use Illuminate\Auth\EloquentUserProvider;
class MyEloquentUserProvider extends EloquentUserProvider
{
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials)
{
if (empty($credentials)) {
return;
}
// $credentials will be an array that looks like:
// [
// 'api_token' => 'token-value',
// ]
// $this->createModel() will give you a new instance of the class
// defined as the model in the auth config for your application.
// Your logic to find the user with the given token goes here.
// Return found user or null if not found.
}
}
创建 class 后,您需要让 Auth
知道。您可以在 AuthServiceProvider
服务提供商的 boot()
方法中执行此操作。此示例将使用名称 "myeloquent",但您可以使用任何您想要的名称("eloquent" 和 "database" 除外)。
public function boot()
{
$this->registerPolicies();
Auth::provider('myeloquent', function($app, array $config) {
return new \App\Services\Auth\MyEloquentUserProvider($app['hash'], $config['model']);
});
}
最后,您需要告诉 Auth
使用新的 myeloquent
用户提供商。这是在 config/auth.php
配置文件中完成的。
'providers' => [
'users' => [
'driver' => 'myeloquent', // this is the provider name defined above
'model' => App\User::class,
],
],
您可以在 documentation here.
中阅读有关添加自定义用户提供商的更多信息从版本Laravel 5.7.28开始,您可以在config/auth.php
中简单地设置。
'guards' => [
'api' => [
'driver' => 'token',
'input_key' => 'token', // The input name to pass through
'storage_key' => 'token', // The column name to store in database
'provider' => 'users',
],
],