且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

防止未经授权的用户访问laravel 5中的管理页面

更新时间:2022-03-30 19:45:40

通过@craig_h答案和我的研究,我发现必须在另一个路由组中分离登录和注销路由.当我使用此代码时:

By @craig_h answer and my researches, I found that I must to separate Login and Logout Routes in another Route Group. when I used this code :

Route::group(
    array (
        'prefix' => 'admin',
        'middleware' => ['auth']
    ),
    function () {
        Route::resource('posts', 'postController');

        Route::get('/login', array ('uses' => 'loginController@showForm'));
        Route::post('/login', array ('uses' => 'loginController@checkLogin'));

        Route::get('/logOut', array ('uses' => 'loginController@doLogout'));

    }
);

我得到此网页具有重定向循环,Chrome中出现错误,因为登录和注销位于发布资源路由的同一路由组中,并且未经授权的用户返回登录页面laravel尝试对他进行身份验证,并在页面中发生了重定向循环.

i get This webpage has a redirect loop Error in Chrome because login and logout were in the same Route group that post Resource Route was and when an unauthorized user Returned to login page laravel tries to authenticate him and occurred a redirect loop in the page.

但是当在另一个路由组(例如波纹管)中单独登录和注销路由时,问题解决了,并且一切正常.

but when separate login and logout Route in another route group like bellow,the problem solved and all things worked fine.

Route::group(
    array (
        'prefix' => 'admin',
        'middleware' => ['auth']
    ),
    function () {
        Route::resource('posts', 'postController');
    }
);

Route::group(
    array (
        'prefix' => 'admin'
    ),
    function () {
        Route::get('/login', array ('uses' => 'loginController@showForm'));
        Route::post('/login', array ('uses' => 'loginController@checkLogin'));

        Route::get('/logOut', array ('uses' => 'loginController@doLogout'));

    }
);