我正在尝试使用
"tymon/jwt-auth": "^1.0"
添加 JWT 到 Laravel Framework 7.26.1
。已按照文档设置一切。 https://jwt-auth.readthedocs.io/en/develop/quick-start/
这是我的AuthController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
class AuthController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
/**
* Get a JWT via given credentials.
*
* @return \Illuminate\Http\JsonResponse
*/
public function login()
{
$credentials = request(['email', 'password']);
if (! $token = Auth::attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
...
这是 api.php
Route::post('login', 'AuthController@login')->middleware('api');
Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');
当试图从邮递员那里打
http://127.0.0.1:8000/api/login/
时,这就是我得到的
{
"message": "Method Illuminate\\Auth\\SessionGuard::factory does not exist.",
"exception": "BadMethodCallException",
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php",
"line": 103,
"trace": [
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php",
"line": 307,
"function": "__call",
"class": "Illuminate\\Auth\\SessionGuard",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/app/Http/Controllers/AuthController.php",
"line": 81,
"function": "__call",
"class": "Illuminate\\Auth\\AuthManager",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/app/Http/Controllers/AuthController.php",
"line": 34,
"function": "respondWithToken",
"class": "App\\Http\\Controllers\\AuthController",
"type": "->"
},
{
"function": "login",
"class": "App\\Http\\Controllers\\AuthController",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",
"line": 54,
"function": "call_user_func_array"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
"line": 45,
"function": "callAction",
"class": "Illuminate\\Routing\\Controller",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
"line": 239,
"function": "dispatch",
"class": "Illuminate\\Routing\\ControllerDispatcher",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
"line": 196,
"function": "runController",
"class": "Illuminate\\Routing\\Route",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 685,
"function": "run",
"class": "Illuminate\\Routing\\Route",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 128,
"function": "Illuminate\\Routing\\{closure}",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php",
"line": 41,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Routing\\Middleware\\SubstituteBindings",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php",
"line": 59,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Routing\\Middleware\\ThrottleRequests",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 103,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 687,
"function": "then",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 662,
"function": "runRouteWithinStack",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 628,
"function": "runRoute",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 617,
"function": "dispatchToRoute",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 165,
"function": "dispatch",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 128,
"function": "Illuminate\\Foundation\\Http\\{closure}",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
"line": 21,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
"line": 21,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
"line": 27,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php",
"line": 63,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/fruitcake/laravel-cors/src/HandleCors.php",
"line": 57,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Fruitcake\\Cors\\HandleCors",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/fideloper/proxy/src/TrustProxies.php",
"line": 57,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"function": "handle",
"class": "Fideloper\\Proxy\\TrustProxies",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 103,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 140,
"function": "then",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 109,
"function": "sendRequestThroughRouter",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/public/index.php",
"line": 55,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
},
{
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/server.php",
"line": 21,
"function": "require_once"
}
]
}
对我来说,它似乎正在尝试使用我不明白的网络防护,因为我正在 api.php 中访问路由。有人知道我犯了什么错误吗?
好的。所以显然你必须在expires_in中指定guard
'expires_in' => auth('api')->factory()->getTTL() * 60
我将
respondWithToken
的AuthController.php
功能更新为
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
//'expires_in' => auth()->factory()->getTTL() * 60
'expires_in' => auth('api')->factory()->getTTL() * 60
]);
它修复了它。
正确,但由于您正在处理 API,您可以将
web
中的默认防护从 api
更改为 config/auth.php
,如下所示:
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
然后你可以使用 auth 而不指定守卫:
'expires_in' => auth()->factory()->getTTL() * 60
您可能还需要运行
php artisan optimize
来清除缓存,一切都应该很好。
如果您要为 graphql 应用程序实现此功能,请确保将其包含在您的
config/auth.php
中
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
并且也不要忘记在 User.php 模型中实现
implements JWTSubject
class User extends Authenticatable implements JWTSubject
{ use HasApiTokens, HasFactory, Notifiable;
public function getJWTIdentifier () {
return $this->getKey();
}
public function getJWTCustomClaims () {
return [];
}
}
尝试重新安装软件包,例如“composer require tymon/jwt-auth” 或者只是 Composer dump-autoload 来重新排序库