简体   繁体   English

在Laravel 5.5中进行会话时如何自动重定向到登录页面

[英]How to redirect to a login page automatically when session out in laravel 5.5

I've following configuration of session in config/session.php 我在config/session.php遵循会话的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,

I've made session expire when user is inactive for 5 minutes and redirect to login. 当用户闲置5分钟并重定向到登录时,我已经使会话过期。 It works for all routes and redirect user to login but after session expire when user sends logout request it shows 它适用于所有路由并重定向用户以登录,但是在会话过期后,当用户发送注销请求时,它将显示

 The page has expired due to inactivity.  Please refresh and try again. 

For all other routes it works correctly. 对于所有其他路由,它可以正常工作。

What I should do to to fix this ? 我应该怎么做才能解决这个问题?

NOTE: I've already seen following questions. 注意:我已经看到以下问题。 None works for me. 没有一个适合我。

Redirect automatically when user session expires in Laravel 5.5 当用户会话在Laravel 5.5中到期时自动重定向

Check for Session timeout in Laravel 在Laravel中检查会话超时

You can protect your every route to your middleware. 您可以保护通往中间件的每条路线。

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 () {
//
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM