问题描述
我将带有jwt的Lavarel 5.2框架用于授权
jwt仅使用一种模型即可获取用户信息形式的令牌,现在如何在多个模型上使用jwt解析用户令牌?
对于示例,当我在api jwt中使用客户令牌解析来自客户模型的令牌时,默认保护应为customer
auth.php
:
I use Lavarel 5.2 framework with jwt for authorization
jwt takes user info form token just with one model,now how can i parse user token with jwt on multiple model?
For sample when i use customer token in a api jwt parse that token from customer model , default guard should be customer
auth.php
:
'defaults' => [
'guard' => 'operator',
'passwords' => 'operators',
],
'guards' => [
'operator' => [
'driver' => 'session',
'provider' => 'operators',
],
'customer' => [
'driver' => 'session',
'provider' => 'customers',
],
'biker' => [
'driver' => 'session',
'provider' => 'bikers',
]
],
'providers' => [
'operators' => [
'driver' => 'eloquent',
'model' => App\Http\Services\Auth\Model\User::class,
],
'customers' => [
'driver' => 'eloquent',
'model' => App\Http\Aggregate\Customer\Model\Customer::class,
],
'bikers' => [
'driver' => 'eloquent',
'model' => App\Http\Aggregate\Biker\Model\Biker::class,
]
],
推荐答案
您可以创建一个单独的中间件,例如AuthModel.这样,您可以将配置设置为采用如下所示的提供程序,
You can create a separate middleware like AuthModel. In that you can set the config to take which providers like the below,
Config::set('auth.providers.users.model',\App\Models\Customer::class);
如果要使用多个模型,则需要使用if条件来检查哪个url可以访问哪些模型.可能是这样,
If you want to use multiple models, then need to use if conditions to check which url can access which models. It can be like,
if(url == '/customer/api/') {
Config::set('auth.providers.users.model',\App\Models\Customer::class);
} else if(url == '/biker/api/') {
Config::set('auth.providers.users.model',\App\Models\Biker::class);
}
在上面的示例中,我仅使用了url,因此请从请求中获取它.
In the above example, I have used url just for example, so get it from the request.
这篇关于JWT多种型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!