Laravel 7:重定向到不同警卫的不同登录名
Posted
技术标签:
【中文标题】Laravel 7:重定向到不同警卫的不同登录名【英文标题】:Laravel 7 : Redirect to different logins on different guards 【发布时间】:2020-11-02 09:05:41 【问题描述】:我正在使用 auth 中间件并创建和管理保护来控制管理员访问。我在尝试访问管理员路由时遇到了一些问题,我想将与管理员路由关联的未经身份验证的流量重定向到 /admin/login 页面,但不是那样,而是将我重定向到 /login 页面。 我不知道如何在 Authenticate 类中获取与路由相关联的警卫。
protected function redirectTo($request)
if (! $request->expectsJson())
return route('login');
这就是代码,我想是这样的:
protected function redirectTo($request)
if (! $request->expectsJson())
if(Auth::guard('admin'))
return route('admin.login);
else
return route(login);
但它不起作用,因为我唯一的参数是 $request。
这些是我的路线...
//Admin Routes
Route::middleware(['auth:admin'])->group(function ()
Route::get('/admin', 'AdminController@index')->name('admin');
Route::get('/newDoncente', 'AdminController@addDocenteView')->name('newDocente');
//Docentes Routes
Route::get('/docentes', 'Docente\DocenteController@getDocentesView')->name('getDocentesView');
Route::get('/editDocente/id', 'Docente\DocenteController@editDocenteView')->name('editDocentesView');
Route::get('/docentesTables', 'Docente\DocenteController@getDocentesDatatables')->name('getDocentesTables');
Route::get('/docente/id', 'Docente\DocenteController@getDocenteView')->name('getDocenteView');
谢谢。
【问题讨论】:
您可以根据路线放置条件。 怎么样?我可以修补它,将 /admin/ 路径添加到所有路由...'因为请求我可以获得路径,但我不知道这是否正确。 【参考方案1】:我不确定这是否是正确的方法,我覆盖了 handle 方法,以便可以按如下方式从中获取守卫:
protected $guards = [];
public function handle($request, Closure $next, ...$guards)
$this->guards = $guards;
return parent::handle($request, $next, ...$guards);
protected function redirectTo($request)
if (in_array("admin", $this->guards))
return "admin/login";
【讨论】:
【参考方案2】:我一直在寻找基于警卫的答案,我希望检查警卫而不是基于警卫类型的重定向。但是我找不到解决方案。
但是,我找到了另一种解决方案,可以完成工作,但它不像我正在寻找的答案那样动态。但是,它对我来说很好用
在
App\Http\Middleware\Authincate.php
类,添加如下代码:
protected function redirectTo($request)
if (! $request->expectsJson())
if($request->is('admin') || $request->is('admin/*'))
return route('admin.login');
return route('login');
另外,不要忘记为所有管理路由添加前缀 admin/ 。 希望这个答案对您有所帮助。
【讨论】:
【参考方案3】:进入app/Exception/Handler.php
使用这个类
use Illuminate\Auth\AuthenticationException;
并在类中添加这个函数
protected function unauthenticated($request, AuthenticationException $exception)
if (request()->expectsJson())
return Response()->json(['error' => 'UnAuthorized'], 401); //exeption for api
$guard = data_get($exception->guards(), 0);
switch ($guard)
case 'admin':
$login = 'admin.login';
break;
default:
$login = 'login';
break;
return redirect()->guest(route($login));
【讨论】:
【参考方案4】:好的,我已经解决了,我想这是一个很好的方法。我创建了一个中间件,并要求在中间件上使用 auth 保护。
首先我创建中间件
class IsAdmin
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
if (Auth::guard('admin')->check())
return $next($request);
else
return redirect('/admin/login');
然后我把中间件放到Kernel文件中
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'isAdmin' => \App\Http\Middleware\IsAdmin::class,
最后,我更改了路由的中间件
Route::get('/admin', 'AdminController@index')->name('admin');
现在它工作正常,但如果你有更好的解决方案,我真的很想知道。
【讨论】:
以上是关于Laravel 7:重定向到不同警卫的不同登录名的主要内容,如果未能解决你的问题,请参考以下文章