luoxiaojun1992 / laravel-tars

Tars driver for laravel.
Apache License 2.0
28 stars 9 forks source link

用了lumen自带的api守卫发到tars平台上,第一次访问的时候正常,第二次就提示如下错误,本地用nginx一切正常 #25

Closed wx1860 closed 3 years ago

wx1860 commented 3 years ago
umen.ERROR: InvalidArgumentException: Auth guard driver [api] is not defined. in /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/illuminate/auth/AuthManager.php:97
Stack trace:
#0 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/illuminate/auth/AuthManager.php(68): Illuminate\Auth\AuthManager->resolve('api')
#1 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/app/Http/Middleware/Authenticate.php(41): Illuminate\Auth\AuthManager->guard('api')
#2 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/illuminate/pipeline/Pipeline.php(149): App\Http\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure))
#3 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request))
#4 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#5 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/app/Http/Middleware/ApiMiddleware.php(37): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing{closure}(Object(Illuminate\Http\Request))
#6 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/illuminate/pipeline/Pipeline.php(149): App\Http\Middleware\ApiMiddleware->handle(Object(Illuminate\Http\Request), Object(Closure))
#7 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request))
#8 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#9 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/illuminate/pipeline/Pipeline.php(102): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing{closure}(Object(Illuminate\Http\Request))
#10 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(410): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#11 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(256): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#12 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(160): Laravel\Lumen\Application->handleFoundRoute(Array)
#13 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\Concerns{closure}(Object(Illuminate\Http\Request))
#14 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#15 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/fruitcake/laravel-cors/src/HandleCors.php(37): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing{closure}(Object(Illuminate\Http\Request))
#16 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/illuminate/pipeline/Pipeline.php(149): Fruitcake\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure))
#17 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request))
#18 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#19 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/illuminate/pipeline/Pipeline.php(102): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing{closure}(Object(Illuminate\Http\Request))
#20 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(410): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#21 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(166): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#22 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/luoxiaojun1992/laravel-tars/src/Route/TarsRoute.php(52): Laravel\Lumen\Application->dispatch(Object(Illuminate\Http\Request))
#23 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/luoxiaojun1992/laravel-tars/src/Route/TarsRoute.php(27): Lxj\Laravel\Tars\Route\TarsRoute->handle(Object(Tars\core\Request))
#24 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/phptars/tars-server/src/protocol/HTTPProtocol.php(35): Lxj\Laravel\Tars\Route\TarsRoute->dispatch(Object(Tars\core\Request), Object(Tars\core\Response))
#25 /data/tars/tarsnode-data/Kdy.RpcClient/bin/src/vendor/phptars/tars-server/src/core/Server.php(675): Tars\protocol\HTTPProtocol->route(Object(Tars\core\Request), Object(Tars\core\Response))
#26 {main}
luoxiaojun1992 commented 3 years ago

bootstrap/App.php 里边 AuthServiceProvider 注册了吗?

wx1860 commented 3 years ago

注册了,发布到tars平台第一次请求成功,第二次的时候就报错,在本地用nginx代理毫无问题

luoxiaojun1992 commented 3 years ago

src/app/Providers/AuthServiceProvider.php

image

src/routes/web.php

image

image

@wx1860 没有重现

wx1860 commented 3 years ago

public function boot() { // Here you may define how you wish users to be authenticated for your Lumen // application. The callback which receives the incoming request instance // should return either a User instance or null. You're free to obtain // the User instance via an API token or any other method necessary.

    $this->app['auth']->viaRequest('api', function ($request) {
        //先取cookie
        $jwt = Cookie::get('jwt');
        if(!$jwt){
            //从hearde取
            $jwt = $request->header('token');
        }
        if($jwt){
            Log::debug("Authorize:" . $jwt);
            return $this->getUser($jwt);
        }
   });
}

对AuthServiceProvider的boot方法改了一下

luoxiaojun1992 commented 3 years ago

public function boot() { // Here you may define how you wish users to be authenticated for your Lumen // application. The callback which receives the incoming request instance // should return either a User instance or null. You're free to obtain // the User instance via an API token or any other method necessary.

    $this->app['auth']->viaRequest('api', function ($request) {
        //先取cookie
        $jwt = Cookie::get('jwt');
        if(!$jwt){
            //从hearde取
            $jwt = $request->header('token');
        }
        if($jwt){
            Log::debug("Authorize:" . $jwt);
            return $this->getUser($jwt);
        }
   });
}

对AuthServiceProvider的boot方法改了一下

这个修改看上去与对接TarsPHP无关。 你可以用composer更新下laravel-tars,我这边没有出现你反馈的问题了。

wx1860 commented 3 years ago

多谢,更新到0.1.10问题解决,之前用的0.1.5版本