Lumen Jwt 身份验证获取(参数 2 传递给 Tymon\JWTAuth\JWTGuard)错误
Posted
技术标签:
【中文标题】Lumen Jwt 身份验证获取(参数 2 传递给 Tymon\\JWTAuth\\JWTGuard)错误【英文标题】:Lumen Jwt authentication get (Argument 2 passed to Tymon\JWTAuth\JWTGuard) errorLumen Jwt 身份验证获取(参数 2 传递给 Tymon\JWTAuth\JWTGuard)错误 【发布时间】:2021-05-05 15:52:12 【问题描述】:我正在创建一个使用 JWT 身份验证的流明 API,我执行了 JWT 文档中的所有步骤,如下所示,但是当我测试它时出现错误:
用户模型
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Model implements JWTSubject
protected $fillable = [
'username',
'password',
'email',
'fullname',
'natId',
'role',
];
public function Predector()
return $this->hasMany('App\Models\Predect');
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
return $this->getKey();
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
return [];
AuthController
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
class AuthController extends Controller
//-----------------------------register users ---------------------------------
public function register(Request $request)
$password = password_hash($request->password, PASSWORD_DEFAULT);
$users_count= User::where('username',$request->username)->get();
$email_count= User::where('email',$request->email)->get();
$credentials = request(['username', 'password']);
dd(auth()->attempt($credentials));
if(count($users_count)>0)
$data = [
'status' => 'username found',
];
else
if (count($email_count)>0)
$data = [
'status' => 'email is found',
];
else
$user = User::create(
[
'username'=>$request->username,
'password'=>$password,
'email'=>$request->email,
'fullname'=>$request->fullname,
'natId'=>$request->nat_id,
'role'=>'user'
]);
if($user)
$credentials = request(['username', 'password']);
if (! $token = auth()->attempt($credentials))
return response()->json(['error' => 'Unauthorized'], 401);
else
$data = [
'username'=>$request->username,
'password'=>$password,
'email'=>$request->email,
'fullname'=>$request->fullname,
'natId'=>$request->nat_id,
'status' => 'success',
'token' => $token
];
else
$data = [
'status' => 'failed',
];
return $data;
bootstrap/app.php,我添加了这些:
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
'admin.auth' => \App\Http\Middleware\AdminMiddleware::class,
]);
$app->register(App\Providers\AuthServiceProvider::class);
$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);
我的路线:
$router->group(['prefix'=>'api'], function () use ($router)
$router->post('/register','AuthController@register');
$router->post('/login','AuthController@login');
);
当我在 Insomnia 中测试它时,我得到了这个错误:
Argument 2 passed to Tymon\JWTAuth\JWTGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given, called in E:\Laravel\euro2021\vendor\tymon\jwt-auth\src\Providers\AbstractServiceProvider.php on line 102
我在我的步骤中错过了什么?解决办法是什么?
新
congig/auth.php
<?php
return [
'defaults' => [
'guard' => env('AUTH_GUARD', 'api'),
'passwords' => 'users',
],
'guards' => [
'api' => [
'driver' => 'jwt',
'provider' => 'users'
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
]
],
'passwords' => [
//
],
];
【问题讨论】:
【参考方案1】:在您的 config/auth.php
中,您可能缺少设置 provider
并引用这样的守卫
'defaults' => [
'guard' => env('AUTH_GUARD', 'api'),
'passwords' => 'users',
],
'guards' => [
'api' => [
'driver' => 'jwt',
'provider' => 'users'
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
]
],
这在JWT documentation 中有描述。
此外,您的用户模型需要扩展 Authenticable
use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
use Authenticatable, Authorizable;
// .. more
【讨论】:
我明白了:Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\Models\User given, called in E:\Laravel\euro2021\vendor\tymon\jwt-auth\src\JWTGuard.php on line 390
你能在上面的问题中分享你的config/auth.php
吗?
我可以谢谢
我添加了 auth.php 文件
我从头开始,按照你说的做了修改,成功了,非常感谢以上是关于Lumen Jwt 身份验证获取(参数 2 传递给 Tymon\JWTAuth\JWTGuard)错误的主要内容,如果未能解决你的问题,请参考以下文章
Jwt身份验证错误参数3传递给Lcobucci JWT Signer Hmac :: doVerify()
如何使用相同的身份验证保护验证多种类型的用户 [Lumen]