一个路由使用多个认证中间件

Using multiple authentication middlewares for one route

我有一个要求,其中有一个 API 方法(由众所周知的包 tymon/jwt-auth 保护),我还需要能够使用基于 [= 的基本会话访问它12=] 中间件.

我不想在 api.phpweb.php 中重复路线,即使那样完全可行。

我尝试将两者都添加到路由中,但它们根本不起作用,例如:['auth:api', 'web'] 我还尝试创建一个新的中间件,目的是像这样检查 apiweb

class CombinedAuth
{
    public function handle($request, Closure $next)
    {
        $web = Auth::guard('web')->user();
        if ($web) {
            return $next($request);
        }

        $api = Auth::guard('api')->user();
        if ($api) {
            return $next($request);
        }

        if(!$web && !$api) {
            throw new AuthorizationException;
        }
    }
}

这也行不通。 api 中间件工作正常,但 web 中间件没有,它总是让我退出并重定向到登录页面。

那么在Laravel 5.8中有没有一种同时使用apiweb中间件保护路由的巧妙方法?

您可以使用'auth:api,web'来检查多个守卫。

在 Laravel 9 路由中使用多个 can 中间件调用;

<?php

Route::get('/', function () {
    return view('money');
})
->middleware([
    'can:manage,App\Models\Payment', 
    'can:manage,App\Models\Withdraw',
]);


?>