php - 如何在laravel 5.5中会话时自动重定向到登录页面
问题描述
我有以下会话的配置config/session.php
/*
|--------------------------------------------------------------------------
| Session Lifetime
|--------------------------------------------------------------------------
|
| Here you may specify the number of minutes that you wish the session
| to be allowed to remain idle before it expires. If you want them
| to immediately expire on the browser closing, set that option.
|
*/
'lifetime' => env('SESSION_LIFETIME', 5),
'expire_on_close' => true,
当用户处于非活动状态 5 分钟并重定向到登录时,我已使会话过期。它适用于所有路由并将用户重定向到登录,但在会话到期后,当用户发送注销请求时它显示
The page has expired due to inactivity. Please refresh and try again.
对于所有其他路线,它可以正常工作。
我应该怎么做才能解决这个问题?
注意:我已经看到以下问题。没有一个对我有用。
解决方案
您可以保护通往中间件的每条路线。
protected $middleware = [
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
'Illuminate\Cookie\Middleware\EncryptCookies',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
'Illuminate\Session\Middleware\StartSession',
'Illuminate\View\Middleware\ShareErrorsFromSession',
'App\Http\Middleware\VerifyCsrfToken',
'App\Http\Middleware\Authenticate',// add this line according to your namespace
];
it will redirect the user if not logged in. UPDATE Keep in mind that adding auth middleware as global will create redirect loop so avoid it.
Or if you want specific routes to be protected then attach the middleware auth to that route
Route::get('admin/profile', ['middleware' => 'auth', function () {
//
}]);
推荐阅读
- android - 为什么导航抽屉菜单项在运行时在 android 9 上更新,但相同的代码不适用于 android 11?
- scala - ScalaMock如何使用名称参数模拟方法
- flutter - 颤动的未来
?> 使用 Firestore - groovy - 解析和修改 JSON
- python - 我有一个嵌套的 json,我想运行一个 for 循环以仅匹配不同的键
- angular - 手动更新 ngrx-forms 的 FormGroupState 会在下一次状态更改时恢复
- javascript - 在 NextJs 中动态创建主页
- python - 如何检查pyspark数组列中单词列表的计数?
- go - 如何覆盖返回值?
- javascript - 为什么使用结果之间存在差异