流明护照返回500错误而不是401错误

时间:2019-09-16 08:14:45

标签: laravel lumen

我正在尝试将流明护照用于我们的项目。我已经按照此lumen-passport进行了安装。我通过执行邮递员请求成功接收到access_token,这是我的代码。

{
 "grant_type": "client_credentials",
 "client_id": "2",
 "client_secret": "1QaxEr6P3K6kKsqa63nA2SMLsczuJJRvufXEDrzY",
 "scope": "*"
}

我收到访问令牌

{
"token_type": "Bearer",
"expires_in": 120,
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImJmY2YyYjZlM2I4MTlkMzI3OTE5ODgyMjM0M2NlNjcxM2MyYjFkMjJjZDFiNTQ5ODQ5MjkxMzllMDc2NWJmM2UyZGMyMTMxYTRmYzk1MzNkIn0.eyJhdWQiOiIyIiwianRpIjoiYmZjZjJiNmUzYjgxOWQzMjc5MTk4ODIyMzQzY2U2NzEzYzJiMWQyMmNkMWI1NDk4NDkyOTEzOWUwNzY1YmYzZTJkYzIxMzFhNGZjOTUzM2QiLCJpYXQiOjE1Njg2MTg4ODksIm5iZiI6MTU2ODYxODg4OSwiZXhwIjoxNTY4NjE5MDA5LCJzdWIiOiIiLCJzY29wZXMiOlsiKiJdfQ.hIXxn1MJMSS6m2XXrt-EJatMp7KoomzKYnk_McpeeBo9VMYUtKU7tPWCmtw7XpFZFJWvGhIY8cx_A6kTaizFjqEkKmlj3jpjs9X9QUZNQ4J5CwjIcXUAJLRqw6WYvvW94GdPAgUBFz6eIbg_Tzt-149dIwPzlVpd8Ln3Bu84Htj9tiWalkdu5EL6lO_Mc8mFJpAh63fJs84_ES02ex_MACsO52pwfXQLPdrjyWSHPdqDj0hRPVMVMLtPdNtYS12MVf8xg_C6KdcB19viRhmlnQwjl98AXGYt_YeeJozKQax2bSSTqTHfNrBZpka7FZFMznkS3gQ8-9d9FYNSNu3Hiia2ZN44JkolBXB4bNpaa82cj_2yrQ-w8oFivOQqX-dQU7RDD5womVbCd8VmxmtUoTTXdWjridl0F5XasWJfc1N2vTGleY_AGi1qC5_39QXTXsEeesgsiGvq4OJCeIRSLvuuwpFUbV7LVfUvJVL3HSM9-PUBDw0Q2q7mCVLMf_ZLf5s_rNP7uTZOIpZUvUKYXCk4rCB6dE7YIGQZkBr2sOQADd8foMvnxEatwIiJoQCxAEHRgeybmBk4gDUhr-9IoroHxnXrcg2LPulfwe5-hS8cAkjh7OvUuld8W17_TzWOUJq1XPpgWCRomSAB_KeXdyFzrGyVXJTD2r_DU1HbJGE"}

在我的路线上,我有这些代码。

$router->group(['prefix' => 'api/v1', 'middleware' => 'client'], function() use (&$router){
$router->get('example/{id}', 'ExampleController@getData');});

为什么每次令牌过期时都会得到500错误而不是401错误?

我检查了存储日志中的日志,这是错误。我不知道这是否真的是错误。

[2019-09-17 02:39:59] local.ERROR: Illuminate\Auth\AuthenticationException: Unauthenticated. in /var/www/scheduler-app/vendor/laravel/passport/src/Http/Middleware/CheckClientCredentials.php:48
Stack trace:
#0 /var/www/scheduler-app/vendor/illuminate/pipeline/Pipeline.php(163): Laravel\Passport\Http\Middleware\CheckClientCredentials->handle(Object(Laravel\Lumen\Http\Request), Object(Closure))
#1 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Laravel\Lumen\Http\Request))
#2 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Laravel\Lumen\Http\Request))
#3 /var/www/scheduler-app/vendor/illuminate/pipeline/Pipeline.php(104): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing\{closure}(Object(Laravel\Lumen\Http\Request))
#4 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(413): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#5 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(259): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#6 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(233): Laravel\Lumen\Application->handleFoundRoute(Array)
#7 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(169): Laravel\Lumen\Application->handleDispatcherResponse(Array)
#8 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(416): Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(Object(Laravel\Lumen\Http\Request))
#9 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(171): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#10 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(108): Laravel\Lumen\Application->dispatch(NULL)
#11 /var/www/scheduler-app/public/index.php(28): Laravel\Lumen\Application->run()
#12 {main} {"exception":"[object] (Illuminate\\Auth\\AuthenticationException(code: 0): Unauthenticated. at /var/www/scheduler-app/vendor/laravel/passport/src/Http/Middleware/CheckClientCredentials.php:48)
[stacktrace]
#0 /var/www/scheduler-app/vendor/illuminate/pipeline/Pipeline.php(163): Laravel\\Passport\\Http\\Middleware\\CheckClientCredentials->handle(Object(Laravel\\Lumen\\Http\\Request), Object(Closure))
#1 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Laravel\\Lumen\\Http\\Request))
#2 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Laravel\\Lumen\\Http\\Request))
#3 /var/www/scheduler-app/vendor/illuminate/pipeline/Pipeline.php(104): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Laravel\\Lumen\\Http\\Request))
#4 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(413): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#5 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(259): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))
#6 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(233): Laravel\\Lumen\\Application->handleFoundRoute(Array)
#7 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(169): Laravel\\Lumen\\Application->handleDispatcherResponse(Array)
#8 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(416): Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}(Object(Laravel\\Lumen\\Http\\Request))
#9 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(171): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))
#10 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(108): Laravel\\Lumen\\Application->dispatch(NULL)
#11 /var/www/scheduler-app/public/index.php(28): Laravel\\Lumen\\Application->run()
#12 {main}

2 个答案:

答案 0 :(得分:0)

对于那些知道流明护照的开发人员,我知道这不是最好的答案,但是我想出了另一种解决办法。我刚刚从CheckClientCredentials复制了代码,正如我所检查的那样,代码验证了请求。这是

public function handle($request, Closure $next, ...$scopes)
    {
        $psr = (new DiactorosFactory)->createRequest($request);

        try {
            $psr = $this->server->validateAuthenticatedRequest($psr);
        } catch (OAuthServerException $e) {
            throw new AuthenticationException;
        }

        $this->validateScopes($psr, $scopes);

        return $next($request);
    }

在我的身份验证中间件中,添加以下代码。

public function handle($request, Closure $next, $guard = null)
    {
        $psr = (new DiactorosFactory)->createRequest($request);

        try {
            $psr = $this->server->validateAuthenticatedRequest($psr);
        } catch (OAuthServerException $e) {
            return response('Unauthorized.', 401);
        }

        // if ($this->auth->guard($guard)->guest()) {
        //     return response('Unauthorized.', 401);
        // }

        return $next($request);
    }

以及我拥有的路线

$router->group(['prefix' => 'api/v1', 'middleware' => ['auth', 'client']], function() use (&$router){
    $router->get('example/{id}', 'ExampleController@getData');
});

现在正在工作。我收到401错误,如果令牌没有过期,它将验证令牌。

答案 1 :(得分:0)

感谢您的解决方案。我添加了检查是否真的是 401 错误。

ALTER trigger trig_db_alter_table on database
for ALTER_TABLE 
as
begin

    declare @d xml = EVENTDATA();

    declare @tblschemaname sysname, 
            @tblname sysname, 
            @action varchar(20),
            @colname sysname,
            @sqlcommand nvarchar(max),
            
            @WHILE_Count int,
            @WHILE_Count_Max int;

    --for multiple columns      
    select 
        @tblschemaname = @d.value('(/EVENT_INSTANCE/SchemaName)[1]', 'sysname'),
        @tblname = @d.value('(/EVENT_INSTANCE/ObjectName)[1]', 'sysname'),
        @action = @d.value('local-name((/EVENT_INSTANCE/AlterTableActionList/*)[1])', 'varchar(20)'),
        --change this for multiple columns (depends on the logging)
        @colname = @d.value('(/EVENT_INSTANCE/AlterTableActionList/*[1]/Columns/Name)[1]', 'sysname'),
        @sqlcommand = @d.value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]', 'nvarchar(max)');

    CREATE TABLE #TMP_list_of_tables_in_DB
        (ID INT IDENTITY(1,1)
        ,object_id_table int
        )

    INSERT INTO #TMP_list_of_tables_in_DB
        (object_id_table)
    SELECT 
        (object_id)
    FROM [DBNAME].sys.objects
        WHERE TYPE IN('P','T','FN','u')
        AND TYPE_DESC = 'USER_TABLE'

    SET @WHILE_Count = 1
    SET @WHILE_Count_Max = (SELECT MAX(ID) FROM #TMP_list_of_tables_in_DB)

    WHILE @WHILE_Count <= @WHILE_Count_Max

    BEGIN

    if object_id(quotename(@tblschemaname) + '.' + quotename(@tblname)) = (SELECT object_id_table FROM #TMP_list_of_tables_in_DB WHERE ID = @WHILE_Count)
      and @colname is not null
    begin
        insert into dbo.logtablechanges
        (
            tableobject_id, 
            tablename, columnname,  theaction, thestatement
        )
        values
        (
            object_id(quotename(@tblschemaname) + '.' + quotename(@tblname)),
            @tblname, @colname, @action, @sqlcommand
        )

    end

    SET @WHILE_Count = @WHILE_Count + 1

    END

    DROP TABLE #TMP_list_of_tables_in_DB

end