我使用Lavarel 5.2框架和jwt进行授权
jwt只使用一个模型获取用户信息表单令牌,
现在如何在多个模型上用jwt解析用户令牌?
对于我在api jwt中使用客户令牌从客户模型解析该令牌的示例,默认后卫应该是客户
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,
]
],
答案 0 :(得分:3)
您可以创建一个单独的中间件,如AuthModel。您可以将配置设置为采用以下哪些提供商
Config::set('auth.providers.users.model',\App\Models\Customer::class);
如果要使用多个模型,则需要使用if条件来检查哪个url可以访问哪些模型。它可以像,
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,因此请从请求中获取它。
答案 1 :(得分:0)
您可以如下更改每个控制器中的__construct
功能。这样jwt知道要验证哪种模型。
BikerController
function __construct()
{
Config::set('jwt.user', Biker::class);
Config::set('auth.providers', ['users' => [
'driver' => 'eloquent',
'model' => Biker::class,
]]);
}
CustomerController
function __construct()
{
Config::set('jwt.user', Customer::class);
Config::set('auth.providers', ['users' => [
'driver' => 'eloquent',
'model' => Customer::class,
]]);
}
答案 2 :(得分:0)
这是我的解决方案。在 Laravel 6 上测试
用户模型
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject
{
use SoftDeletes;
use Notifiable;
public $incrementing = false;
protected $keyType = 'string';
protected $fillable =
[
];
protected $hidden =
[
'password',
'created_at',
'updated_at',
'deleted_at'
];
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
}
}
教师模式
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Tymon\JWTAuth\Contracts\JWTSubject;
class Teacher extends Authenticatable implements JWTSubject
{
use SoftDeletes;
use Notifiable;
public $incrementing = false;
protected $keyType = 'string';
protected $fillable =
[
];
protected $hidden =
[
'password',
'oldpassword',
'created_at',
'updated_at',
'deleted_at'
];
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
}
}
config/auth.php
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users'
],
'teacher-api' => [
'driver' => 'jwt',
'provider' => 'teachers'
],
],
AuthController 函数:
if (
$request->getRequestUri() ===
'OTHER_AUTH_ROUTE'
) {
$credentials = $request->only('username', 'password']);
$token = Auth::shouldUse('teacher-api');
$token = Auth::attempt($credentials);
if (!$token) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
$credentials = $request->only([USERNAME, 'password']);
$token = Auth::attempt($credentials);
if (!$token) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
希望对大家有帮助
答案 3 :(得分:0)
Laravel 8 兼容
对于仍在寻找干净解决方案的其他人:
我建议在 config/auth.php 中手动配置提供者和守卫,而不是以编程方式更改任何提供者。
接下来要确保使用正确的 JWTSubject 身份验证模型,是为一组必须使用的路由创建不同的中间件(不要忘记在 $routeMiddleware
下的 Kernel.php 中指定它)只能由特定的守卫/身份验证模型访问。那么对于 Manager 模型,中间件句柄函数可能如下所示:
public function handle(Request $request, Closure $next) {
if (!($request->user('managers'))) abort(401);
Auth::shouldUse('managers');
return $next($request);
}
然后创建另一个中间件,假设为 Employee 模型,并将 'managers' 保护值更改为您在 config/auth.php 中配置的 'employees'。
在您的 routes/api.php 中,您可以使用(例如)指定一个路由组:
Route::group(['middleware' => 'management'], function() { });
为了使这一切正常工作,请在调用 auth()->attempt()
函数时指定保护,例如auth('managers')->attempt($credentials))
。