我是 Laravel 5 的新手,正在尝试制作一个简单的身份验证页面。我的问题是我可以在单击注销链接后正确注销,但如果我单击浏览器的后退按钮,仍然能够看到页面的内容,实际上不应该看到关于我的身份验证中间件进程的内容。我读到我可以通过禁用缓存来防止这种情况发生,但我认为这不是最好的方法,所以我怎样才能以更好的方式做到这一点?简单地说,我的注销路线是
Route::get('logout', array('uses' => 'LoginController@logout'));
注销函数是:
public function logout() {
Auth::logout(); // logout user
Session::flush();
Redirect::back();
return Redirect::to('pages/login'); //redirect back to login
}
最佳答案
使用 artisan 创建一个中间件:
php artisan make:middleware RevalidateBackHistory
在 RevalidateBackHistory 中间件中,我们将 header 设置为无缓存并重新验证:
<?php
namespace App\Http\Middleware;
use Closure;
class RevalidateBackHistory
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
return $response->header('Cache-Control','nocache, no-store, max-age=0, must-revalidate')
->header('Pragma','no-cache')
->header('Expires','Fri, 01 Jan 1990 00:00:00 GMT');
}
}
在 Kernel.php 中更新应用程序的路由中间件:
protected $routeMiddleware = [
.
.
'revalidate' => \App\Http\Middleware\RevalidateBackHistory::class,
.
.
];
就是这样!所以基本上你只需要为需要用户身份验证的路由调用 revalidate 中间件。
关于php - 在 Laravel 5 注销后防止浏览器的后退按钮登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30118998/