Laravel 登录重定向你太多次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37820237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:59:28  来源:igfitidea点击:

Laravel login redirected you too many times

laravellaravel-5.1laravel-5.2laravel-routing

提问by ScoRpion

I have been struggling with this from quiet a time now, what i am trying is to redirect all the url's hit by non-logged in users to login page and it gives me this error, which I am sure is because it is creating a loop on /login URL. authentication is checking for authorized user in login page also. however I wish the login page should be an exception when checking the auth. I may be doing something wrong which I am not able to get. here goes my code.

我一直在为此苦苦挣扎,我正在尝试将未登录用户点击的所有 url 重定向到登录页面,它给了我这个错误,我确定这是因为它正在创建一个循环在 /login URL 上。身份验证也在检查登录页面中的授权用户。但是我希望在检查身份验证时登录页面应该是一个例外。我可能做错了一些我无法得到的事情。这是我的代码。

routes.php

路由文件

Route::post('login', 'Auth\AuthController@login');
Route::get('login' , 'Auth\AuthController@showLoginForm');
Route::get('/'     , 'Auth\AuthController@showLoginForm');


kernel.php

内核文件

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'can' => \Illuminate\Foundation\Auth\Access\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'acl' => \App\Http\Middleware\CheckPermission::class,
];


Authenticate class

认证类

class Authenticate
{
    public function handle($request, Closure $next, $guard = null) {    
      if (Auth::guard($guard)->guest()) {
        if ($request->ajax() || $request->wantsJson()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('login');
        }
      }
    return $next($request);
    }
}


AuthController class

AuthController 类

class AuthController extends Controller {

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    protected $redirectTo = '/dashboard';
    protected $loginPath = '/login';
    protected $redirectPath = '/dashboard';
public function __construct(){
    $this->middleware('auth', ['except' =>'login']); 
    /* I have been trying these many things to fix this, all in loss.
    // $this->middleware('acl'); // To all methods
    // $this->middleware('acl', ['only' => ['create', 'update']]); 
    // $this->middleware('guest', ['only' => ['/login']]);
    // echo "Message"; exit;
    // $this->middleware('auth');
    // $this->middleware('auth', ['only' => ['login']]);
    // $this->middleware('auth', ['only' => ['/login']]);
    // $this->middleware('auth', ['except' => 'login']);
    // $this->middleware('guest');
    // $this->middleware('guest', ['only' => ['logout' , 'login', '/login', '/']]);
}

Please help me, It going all above my head, seems some sort of rocket science to me. well btw I am new to laravel and may be doing some silly thing around, apologies for that. Thanks in Advance.

请帮助我,这一切都在我的头上,对我来说似乎是某种火箭科学。好吧顺便说一句,我是 laravel 的新手,可能正在做一些愚蠢的事情,为此道歉。提前致谢。

采纳答案by Achraf Khouadja

Why you are doing all this just to redirect every non-logged in user to login form?

为什么你做这一切只是为了将每个未登录的用户重定向到登录表单?

i think you can just do this

我想你可以这样做

Routes.php

路由.php

  Route::post('login', 'Auth\AuthController@login');
  Route::get('login' , 'Auth\AuthController@showLoginForm');
  Route::get('/'     , 'Auth\AuthController@showLoginForm');

Route::group(['middleware' => 'auth'], function () {

    // any route here will only be accessible for logged in users
 });

and auth controller construct should be like this

和 auth 控制器构造应该是这样的

AuthController

验证控制器

public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

回答by Diogo Machado

You need add route login outside Laravel group:

您需要在 Laravel 组之外添加路由登录:

routes.php

路由文件

Route::auth();

Route::group(['middleware' => 'auth'], function () {
  // All route your need authenticated
});

Aditionally, you can see yours route list using:

另外,您可以使用以下方法查看您的路线列表:

php artisan route:list

回答by mkmnstr

The problem is with your routes.

问题出在你的路线上。

When I enter and I am not logged out you send me to login(get) route. And as you are specifying the middleware in the construct function in the AuthController, every time a method of the AuthController is called, construct function is called again and sends you back at login.. and it repeats indefinitely.

当我进入但我没有退出时,您将我发送到登录(获取)路线。并且当您在 AuthController 的构造函数中指定中间件时,每次调用 AuthController 的方法时,都会再次调用构造函数并在登录时将您发送回..并且它无限期地重复。

回答by Mohamed Fahmi Chaar

like @mkmnstr say

就像@mkmnstr 说的

The problem is with your routes. When I enter and I am not logged out you send me to login(get) route. And as you are specifying the middleware in the construct function in the AuthController, every time a method of the AuthController is called, construct function is called again and sends you back at login.. and it repeats indefinitely.

问题出在你的路线上。当我进入但我没有退出时,您将我发送到登录(获取)路线。并且当您在 AuthController 的构造函数中指定中间件时,每次调用 AuthController 的方法时,都会再次调用构造函数并在登录时将您发送回..并且它无限期地重复。

to fix that u should add

解决你应该添加的问题

Auth::logout();

Here

这里

...
} else {
  Auth::logout(); // user must logout before redirect them
  return redirect()->guest('login');
}
...

回答by ajmirani

If your working with custom middleware you must follow it's all rules in my case, I have to define a custom route class in the web middleware group. In the world of copy-paste sometime we make mistakes.

如果您使用自定义中间件,就我而言,您必须遵循所有规则,我必须在 Web 中间件组中定义一个自定义路由类。在复制粘贴的世界中,我们有时会犯错误。

Middleware:

中间件


 public function handle($request, Closure $next)
    {

        if(!isset(session('user'))){
            return redirect('login');
        }

        return $next($request);
    }
}

My Mistake in Kernel.php
if custom middleware class present in web $middlewareGroups will check condition 2 times so it will give error as: redirected you too many times

我在 Kernel.php 中的错误
如果 web $middlewareGroups 中存在自定义中间件类将检查条件 2 次,因此它会给出错误为:重定向你太多次

protected $middlewareGroups = [
        'web' => [ 
                 \App\Http\Middleware\webUser::class, //Remove
        ],

protected $routeMiddleware = [ 
      'webUser'=> \App\Http\Middleware\webUser::class //Keepit  
]