"message": "方法 Illuminate\\Auth\\SessionGuard::factory 不存在。",

Posted

技术标签:

【中文标题】"message": "方法 Illuminate\\\\Auth\\\\SessionGuard::factory 不存在。",【英文标题】:"message": "Method Illuminate\\Auth\\SessionGuard::factory does not exist.","message": "方法 Illuminate\\Auth\\SessionGuard::factory 不存在。", 【发布时间】:2020-12-18 04:02:24 【问题描述】:

我正在尝试使用 "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 中的路由。任何人我犯了什么错误?

【问题讨论】:

好的,这解决了。 github.com/tymondesigns/jwt-auth/issues/1484 【参考方案1】:

好的。所以显然你必须在 expires_in 'expires_in' =&gt; auth('api')-&gt;factory()-&gt;getTTL() * 60 中指定守卫@

我将AuthController.phprespondWithToken功能更新为

return response()->json([
        'access_token' => $token,
        'token_type' => 'bearer',
        //'expires_in' => auth()->factory()->getTTL() * 60
        'expires_in' => auth('api')->factory()->getTTL() * 60
    ]);

它修复了它。

【讨论】:

【参考方案2】:

正确,但由于您正在处理 API,因此您可以将 config/auth.php 中的默认保护从 web 更改为 api,如下所示:

'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
],

然后你可以使用 auth 而不指定守卫: 'expires_in' =&gt; auth()-&gt;factory()-&gt;getTTL() * 60

您可能还需要运行php artisan optimize 来清除缓存,一切都会好起来的。

【讨论】:

【参考方案3】:

如果您正在为 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 [];
 

【讨论】:

以上是关于"message": "方法 Illuminate\\Auth\\SessionGuard::factory 不存在。",的主要内容,如果未能解决你的问题,请参考以下文章

鼠标后的jquery滚动

记一次Maven发布Jar包中文乱码解决方法

java.security.AccessControlException:access denied("java.io.FilePermission" "文件路径&quo

[py]python3编码-接口返回乱码

Error in read.dcf(file.path(pkgname, "DESCRIPTION"), c("Package", "Type&quo

android gson怎么解析这样的json数据 "code":"00","result":"corpid":&quo