注销后如何防止浏览器的后退按钮登录

时间:2015-07-10 06:53:09

标签: laravel laravel-4 laravel-5 laravel-3

我的问题是我点击注销链接后可以正常注销但是如果我点击浏览器的后退按钮,仍然可以看到实际上不应该看到我的身份验证中间件进程的页面内容。 我读过我可以通过禁用缓存来防止这种情况,但不认为这是最好的方法,所以我怎么能以更好的方式做到这一点?我的退出功能是

public function logout()
{
    Auth::logout();
    Session::flush();
    return redirect('login');
}

我的路线是:

Route::get('logout','Homecontroller@logout');

Thanx提前

6 个答案:

答案 0 :(得分:10)

此问题与浏览器有关。浏览器缓存页面内容,并在您按下后退按钮时将缓存的内容提供给用户。

在需要用户登录的页面上设置缓存控制元标记。这样就告诉浏览器不要缓存它。

<强> E.g:

<meta http-equiv="cache-control" content="private, max-age=0, no-cache">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="expires" content="0">

答案 1 :(得分:0)

在页面顶部使用简单的AJAX请求(类似于ping服务),设置缓存false并在其中放入一些子句,以便在未经过身份验证的情况下将访问者重定向到登录。

因此,在注销后,如果您尝试返回,即使主页面被浏览器缓存,它仍会尝试在页面加载时加载AJAX请求。由于用户身份验证不再有效,因此会将用户重定向回登录页面。

答案 2 :(得分:0)

添加此javascript代码,它将阻止重定向。

history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});

答案 3 :(得分:0)

此javascript代码对我有用:

<script>
    // previous page should be reloaded when user navigate through browser navigation
    // for mozilla
    window.onunload = function(){};
    // for chrome
    if (window.performance && window.performance.navigation.type === window.performance.navigation.TYPE_BACK_FORWARD) {
        location.reload();
    }
</script>

在Chrome版本80.0.3987.122(正式版)(64位)和 Firefox 73.0.1(64位)

答案 4 :(得分:0)

使用工匠创建中间件

php artisan make:middleware RevalidateBackHistory

在RevalidateBackHistory中间件中,我们将标头设置为不缓存并重新验证

<?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,
    .
    .
];

仅此而已!因此,基本上,您只需要为需要用户身份验证的路由调用重新验证中间件即可。

答案 5 :(得分:0)

假设您已经使用了内核和中间件

在Controller上,创建一个构造函数,然后添加中间件auth

public function __construct()
{
    $this->middleware('auth');
}

我希望这项工作对您有用