Lumen Jwt 认证获取(参数 2 传递给 Tymon\JWTAuth\JWTGuard)错误
Lumen Jwt authentication get (Argument 2 passed to Tymon\JWTAuth\JWTGuard) error
我正在创建一个 API 与 lumen truing 以使用 JWT 身份验证,我在 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' => [
//
],
];
在您的 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
}
我正在创建一个 API 与 lumen truing 以使用 JWT 身份验证,我在 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' => [
//
],
];
在您的 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
}