中间件身份验证不适用于 Web Laravel 5.2
Posted
技术标签:
【中文标题】中间件身份验证不适用于 Web Laravel 5.2【英文标题】:Middleware auth not working with web Laravel 5.2 【发布时间】:2017-03-15 09:12:05 【问题描述】:您好,我在 laravel 中的身份验证有一些问题。我必须使用两个中间件 1. is web 和 2. auth 。我正在使用网络中间件,以便我可以使用会话来显示 Flash 消息。并希望使用 auth 中间件对用户/管理员进行身份验证。但我面临一些问题。
以下是我检查授权并重定向到各自路线的功能
public function postLoginForm()
$email=Input::get('email');
$password=Input::get('password');
$data=[
'email'=>$email,
'password'=>$password
];
$rules=[
'email'=>'required',
'password'=>'required'
];
$validator=Validator::make($data,$rules);
if($validator->fails())
Session::flash('fail', 'Oops Something went wrong!!');
return redirect()->back()->withErrors($validator);
else
if(Auth::attempt($data))
$checkStatus=User::select('*')->where('email',$email)->first();
Session::put('email',$checkStatus->email);
Session::put('user_type',$checkStatus->user_type);
if($checkStatus['user_type']=='4')
if($checkStatus['status']=='0')
Session::flash('wait', 'Registration is not approved!!');
return "student";
return redirect()->back();
else
return "student else";
return Redirect::route('get.student.dashBoard');
else if($checkStatus['user_type']=='1')
return Redirect::route('get.admin.dashBoard');
else if($checkStatus['user_type']=='2')
return 'admin sir view';
return Redirect::route('get.admin.dashBoard');
else if($checkStatus['user_type']=='3')
return 'admin other view';
return Redirect::route('get.admin.dashBoard');
else
Session::flash('fail', 'Oops Something went wrong!!');
return redirect()->back();
else
Session::flash('fail', 'Login details not matched!!');
return redirect()->back();
return 'nothing works';
以下是我的管理员路线
Route::group(['middleware' => ['web']], function ()
Route::get('/login',
['as' => 'get.login.page',
'uses' => 'LoginController@getLoginPage']);
Route::post('/login-done',
['as' => 'post.login.page',
'uses' => 'LoginController@postLoginForm']);
Route::get('/register',
['as' => 'get.register.page',
'uses' => 'LoginController@getRegisterPage']);
Route::post('/register',
['as' => 'post.register.form',
'uses' => 'LoginController@postRegisterForm']);
Route::get('/forgot-password',
['as' => 'get.forgotPassword.form',
'uses' => 'LoginController@getForgotPasswordForm']);
Route::group(['middleware' => ['auth']], function ()
Route::get('/admin-dashboard',
['as' => 'get.admin.dashBoard',
'uses' => 'admin\PageController@getAdminDashboard']);
Route::get('/all-achievements',
['as' => 'get.achievements',
'uses' => 'admin\AchievementsController@getAchievementsList']);
Route::get('/new-achievement',
['as' => 'get.add.achievement',
'uses' => 'admin\AchievementsController@getAddAchievement']);
Route::post('/add-achievement',
['as' => 'post.achievementsForm',
'uses' => 'admin\AchievementsController@postAchievements']);
Route::get('remove-achievement/achie_slug',
['as' => 'post.delete.achievements',
'uses' => 'admin\AchievementsController@postDeleteAchievement']);
Route::get('edit-achievement/achie_slug',
['as' => 'get.edit.achievements',
'uses' => 'admin\AchievementsController@getEditAchievement']);
Route::post('update-achievement/ach_id',
['as' => 'post.edited.achievement',
'uses' => 'admin\AchievementsController@postEditedAchievement']);
Route::get('/all-news',
['as' => 'get.news.list',
'uses' => 'admin\NewsController@getNewsList']);
Route::get('/add-news',
['as' => 'get.add.news',
'uses' => 'admin\NewsController@getAddNews']);
Route::post('/add-news',
['as' => 'post.add.news',
'uses' => 'admin\NewsController@postAddNews']);
Route::get('/delete-news/news_slug',
['as' => 'get.delete.news',
'uses' => 'admin\NewsController@postDeleteNews']);
Route::get('/edit-news/news_slug',
['as' => 'get.edit.news',
'uses' => 'admin\NewsController@getEditNews']);
Route::post('/edit-news/news_slug',
['as' => 'post.edited.news',
'uses' => 'admin\NewsController@postEditedNews']);
Route::get('/all-admins',
['as' => 'get.admin.list',
'uses' => 'admin\AdminController@getAllAdminList']);
Route::get('/add-admin',
['as' => 'add.new.admin',
'uses' => 'admin\AdminController@getAddNewAdmin']);
Route::post('/add-new-admin',
['as' => 'post.add.new.admin',
'uses' => 'admin\AdminController@postAddNewAdmin']);
Route::get('/all-schedule',
['as' => 'get.timeTable.list',
'uses' => 'admin\TimeTableController@getTimeTableList']);
Route::get('/add-schedule/id',
['as' => 'add.timeTable',
'uses' => 'admin\TimeTableController@getAddNewBatch']);
Route::post('/add-new-batch',
['as' => 'add.newBatch',
'uses' => 'admin\TimeTableController@postAddNewBatch']);
Route::post('/save-year-batch',
['as' => 'save.year.batch',
'uses' => 'admin\TimeTableController@postSaveYearBatch']);
Route::get('/schedule-table/year',
['as' => 'view.schedule.table',
'uses' => 'admin\TimeTableController@getScheduleTable']);
Route::get('/delete-schedule/slug',
['as' => 'delete.schedule.one',
'uses' => 'admin\TimeTableController@postDeleteOneSchedule']);
Route::get('/edit-schedule/slug',
['as' => 'edit.schedule.one',
'uses' => 'admin\TimeTableController@getEditScheduleForm']);
Route::post('/save-edited-schedule/id',
['as' => 'save.edited.schedule',
'uses' => 'admin\TimeTableController@postEditScheduleForm']);
Route::get('/all-results',
['as' => 'get.all.results',
'uses' => 'admin\ResultsController@getAllResults']);
Route::get('/add-result',
['as' => 'get.add.results',
'uses' => 'admin\ResultsController@getAddResult']);
Route::post('/add-new-result',
['as' => 'post.add.result',
'uses' => 'admin\ResultsController@postAddResult']);
Route::get('/delete-result/id',
['as' => 'get.delete.student.result',
'uses' => 'admin\ResultsController@getDeleteResult']);
Route::get('/edit-result/id',
['as' => 'get.edit.student.result',
'uses' => 'admin\ResultsController@getEditResult']);
Route::post('/save-edited-result/id',
['as' => 'post.edited.result',
'uses' => 'admin\ResultsController@postEditedResult']);
Route::get('/contact-messages',
['as' => 'get.contact.message',
'uses' => 'admin\ContactMessageController@getAllContactMessages']);
Route::get('/contact-messages/id',
['as' => 'get.delete.contact.message',
'uses' => 'admin\ContactMessageController@getDeleteContactMessages']);
);
);
每次我尝试登录时,它都会将我重定向到相同的登录页面。请指导我这是怎么回事。
【问题讨论】:
删除其中一个 =>return 'admin other view'; =>return Redirect::route('get.admin.dashBoard');当它返回“admin Other view”时,控件就消失了,它不会执行第二个'return'行 【参考方案1】:您应该来自中间件组的remove web
middleware 以使其正常工作。它自动适用于 web.php
(5.3) 和 routes.php
(5.2.27 及更高版本) 内的所有路由,如果您手动添加它,它将破坏与会话相关的功能。
【讨论】:
我的 Laravel 框架版本 5.2.45 。当我删除网络中间件时,它不允许我使用 Session:flash 来显示 Flash 消息。 @pawankumar,如果你的路由在routes.php
,你应该删除web
中间件。此中间件自动应用于自5.2.27
以来的所有路由。请点击上面答案中的链接。
假设我的某些路由不需要 auth 。 ??
先生,我已经尝试过,但删除 web 中间件没有用。当我像这样使用时,它向我显示了一些错误变量 $errors->first('password') 的错误。
@pawankumar,无论如何,您都应该删除中间件并找出为什么会收到此错误消息。如果您发布一些相关信息,我们将能够为您提供帮助。以上是关于中间件身份验证不适用于 Web Laravel 5.2的主要内容,如果未能解决你的问题,请参考以下文章
身份验证控制器中的 Laravel 5.2 Web 中间件导致 csrf 令牌不匹配