如何在laravel 5.5中会话时自动重定向到登录页面
Posted
技术标签:
【中文标题】如何在laravel 5.5中会话时自动重定向到登录页面【英文标题】:How to redirect to a login page automatically when session out in laravel 5.5 【发布时间】:2019-06-05 00:02:07 【问题描述】:我在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.
对于所有其他路线,它都能正常工作。
我应该怎么做才能解决这个问题?
注意:我已经看过以下问题。没有一个适合我。
Redirect automatically when user session expires in Laravel 5.5
Check for Session timeout in Laravel
【问题讨论】:
签出这个帖子 - github.com/laravel/framework/issues/23212 你可以看到这个解决方案 - laravel-tricks.com/tricks/session-timeout-for-logged-in-user。 【参考方案1】:您可以保护通往中间件的每条路径。
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 ()
//
]);
【讨论】:
以上是关于如何在laravel 5.5中会话时自动重定向到登录页面的主要内容,如果未能解决你的问题,请参考以下文章