Laravel多重身份验证保护路由多个中间件不起作用 [英] Laravel multi auth protecting route multple middleware not working

查看:324
本文介绍了Laravel多重身份验证保护路由多个中间件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个额外的中间件管理员,我想保护自己的路线.添加单个中间件'auth'或'auth:admin'是可行的.

I have created an extra middleware admin and I want to protect my routes. Adding one single middleware 'auth' or 'auth:admin' is working.

Route::get('/calendar', function () {
    return view('app', ['data' => []);
})->middleware('auth');

但是我希望作为管理员,您也可以访问用户路由,但这不起作用.如果尝试以下操作,并且以管理员身份登录,则始终会重定向到登录页面.

But I want that as an admin you can also access the user routes but this is not working. If I try the following, and I log in as an admin I get redirected to the login page all the time.

Route::get('/information', ['middleware' => ['auth', 'auth:admin'], function () {
    return view('app', ['data' => ['auth' => Auth::check()]]);
}]);

但是,如果我将['auth','auth:admin']更改为['auth:admin','auth'],则适用于admin,但不适用于用户.因此,似乎只有我的中间件数组中的第一个元素被识别.有人知道为什么我的多个中间件是分开工作而不是一起工作吗?感谢您的帮助

But if I change ['auth', 'auth:admin'] to ['auth:admin','auth'] it is working for admin but not for user. So it seems that only the first element of my middleware in array is being recognized. Does anybody have any idea why my multiple middlewares are working seperate but not together? Any help is appreciated

推荐答案

如果您试图允许检查多个守卫"的路由,则可以将多个守卫作为参数传递给Authenticate中间件auth .

If you are trying to allow multiple 'guards' to be checked for a route you can pass multiple guards as parameters to the Authenticate middleware, auth.

auth:web,admin(假设web是您的默认保护).

auth:web,admin (assuming web is your default guard).

这将尝试从传入的每个防护中解析用户(Authenticatable).如果任何防护返回了用户(Authenticatable),则您通过了身份验证.如果不是,那么你是客人.

This will try to resolve a user (Authenticatable) from each guard passed in. If any guard returns a user (Authenticatable) you pass through authenticated. If not you are a guest.

如果设置中间件authauth:admin,则它们是堆栈中不相关的2个单独的中间件".

If you set the middleware auth and auth:admin those are 2 separate 'middleware' in the stack that are unrelated.

这篇关于Laravel多重身份验证保护路由多个中间件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆