为什么AuthenticationException异常捕获不到

postman请求返回结果

{
    "message": "Unauthenticated.",
    "status_code": 500,
    "debug": {
        "line": 82,
        "file": "/var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php",
        "class": "Illuminate\\Auth\\AuthenticationException",
        "trace": [
            "#0 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(68): Illuminate\\Auth\\Middleware\\Authenticate->unauthenticated(Object(Dingo\\Api\\Http\\Request), Array)",
            "#1 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(42): Illuminate\\Auth\\Middleware\\Authenticate->authenticate(Object(Dingo\\Api\\Http\\Request), Array)",
            "#2 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Auth\\Middleware\\Authenticate->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure), 'admin')",
            "#3 /var/www/html/laravel-shop/vendor/dingo/api/src/Http/Middleware/PrepareController.php(45): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
            "#4 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Dingo\\Api\\Http\\Middleware\\PrepareController->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
            "#5 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
            "#6 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Routing/Router.php(687): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
            "#7 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Routing/Router.php(662): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Dingo\\Api\\Http\\Request))",
            "#8 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Routing/Router.php(628): Illuminate\\Routing\\Router->runRoute(Object(Dingo\\Api\\Http\\Request), Object(Illuminate\\Routing\\Route))",
            "#9 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Routing/Router.php(617): Illuminate\\Routing\\Router->dispatchToRoute(Object(Dingo\\Api\\Http\\Request))",
            "#10 /var/www/html/laravel-shop/vendor/dingo/api/src/Routing/Adapter/Laravel.php(88): Illuminate\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))",
            "#11 /var/www/html/laravel-shop/vendor/dingo/api/src/Routing/Router.php(518): Dingo\\Api\\Routing\\Adapter\\Laravel->dispatch(Object(Dingo\\Api\\Http\\Request), 'v1')",
            "#12 /var/www/html/laravel-shop/vendor/dingo/api/src/Http/Middleware/Request.php(126): Dingo\\Api\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))",
            "#13 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Dingo\\Api\\Http\\Middleware\\Request->Dingo\\Api\\Http\\Middleware\\{closure}(Object(Dingo\\Api\\Http\\Request))",
            "#14 /var/www/html/laravel-shop/app/Http/Middleware/VerifySign.php(47): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
            "#15 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): App\\Http\\Middleware\\VerifySign->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
            "#16 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
            "#17 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
            "#18 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
            "#19 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
            "#20 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
            "#21 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
            "#22 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(63): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
            "#23 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
            "#24 /var/www/html/laravel-shop/vendor/fruitcake/laravel-cors/src/HandleCors.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
            "#25 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\\Cors\\HandleCors->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
            "#26 /var/www/html/laravel-shop/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
            "#27 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\\Proxy\\TrustProxies->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
            "#28 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
            "#29 /var/www/html/laravel-shop/vendor/dingo/api/src/Http/Middleware/Request.php(127): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
            "#30 /var/www/html/laravel-shop/vendor/dingo/api/src/Http/Middleware/Request.php(103): Dingo\\Api\\Http\\Middleware\\Request->sendRequestThroughRouter(Object(Dingo\\Api\\Http\\Request))",
            "#31 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Dingo\\Api\\Http\\Middleware\\Request->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
            "#32 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
            "#33 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(140): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
            "#34 /var/www/html/laravel-shop/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(109): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))",
            "#35 /var/www/html/laravel-shop/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))",
            "#36 {main}"
        ]
    }
}

php代码:

        // token认证
        try{
            $this->middleware('auth:admin', ['except' => ['getToken','refresh','register']]);
        }catch (\Exception $e) {
            var_dump($e->getMessage());
        }
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
nfangxu
最佳答案

你的PHP代码是在 构造函数 里写的吧

你这样写只能 catch 在注册中间件时的异常 而不是在执行中间件时的异常

如果要捕获这个中间件的异常 你可以在 handler里进行异常捕获

10个月前 评论
讨论数量: 5
nfangxu

你的PHP代码是在 构造函数 里写的吧

你这样写只能 catch 在注册中间件时的异常 而不是在执行中间件时的异常

如果要捕获这个中间件的异常 你可以在 handler里进行异常捕获

10个月前 评论

此中间件是在框架启动阶段执行的,未运行至控制器就已经抛出异常了;
如果你的代码是写在控制器中既产生你上述问题

10个月前 评论

你这个异常捕获的不对,根据你的报错提示,你用这个 Illuminate\\Auth\\AuthenticationException 类去捕捉试试呢

use Illuminate\Auth\AuthenticationException;
try{
            $this->middleware('auth:admin', ['except' => ['getToken','refresh','register']]);
        }catch (AuthenticationException $e) {
            var_dump($e->getMessage());
        }

要是还不行,你就找找 AuthenticationException 这个怎么引入

10个月前 评论

@坐忘 试了AuthenticationException,没什么效果

10个月前 评论

@Joker10000 你这个是中间件报的错,你应该去 auth:admin 这个中间件去看看

10个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!