首页 文章

在Laravel 5中注销后阻止浏览器的后退按钮登录

提问于
浏览
10

我是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
}

5 回答

  • 8

    当用户单击后退按钮时,他们实际登录了_1142568_,它只是浏览器呈现从先前页面视图缓存的内容 . 用户 won't 能够导航或与需要登录的任何内容进行交互,因为对于服务器上的应用程序,他们未经过身份验证 .

    当用户单击 back button 时,您无法控制 it doesn't make a request to the server .

    使用后退按钮,他们能够查看的唯一内容是他们在登录时已经访问过的内容 . 如果他们尝试访问任何新内容,他们会向您的应用程序发出新请求,您的中间件将触发,将它们重定向到登录页面 .

    我想如果你真的想要停止这种行为,你可以使用一些JavaScript等发送ajax请求并检查用户是否以这种方式登录,但从安全的角度来看是无用的 .

  • 0

    这个解决方案很有效!使用工匠创建中间件 .

    php artisan make:middleware RevalidateBackHistory
    

    在RevalidateBackHistory中间件中,我们将标头设置为no-cache并重新验证 .

    <?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中间件 .

  • 23

    步骤1:使用以下命令创建一个中间件:

    php artisan make:middleware PreventBackHistory
    

    第2步:

    用以下内容替换PreventBackHistory.php的内容:

    <?php
    
    namespace App\Http\Middleware;
    
    use Closure;
    
    class PreventBackHistory
    {
        /**
         * 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','no-cache, no-store, max-age=0, must-revalidate')
                ->header('Pragma','no-cache')
                ->header('Expires','Sun, 02 Jan 1990 00:00:00 GMT');
        }
    }
    

    第3步:在kernal.php中注册中间件

    'preventBackHistory' => \App\Http\Middleware\PreventBackHistory::class,
    

    好好去:)

  • 4

    我使用的方法是在注销后简单地重定向到上一页 . 只要上一页受到保护,auth中间件就会启动并重定向回登录页面 . 现在,当您单击后退按钮时,不再缓存上一页,您只需再次获取登录页面 .

    原始讨论:https://laracasts.com/discuss/channels/requests/back-button-browser

    public function logout() {
            Auth::logout(); // logout user
            return redirect(\URL::previous());
    }
    
  • 25

    尝试使用 auth 中间件重定向到受保护的路由:

    return redirect('home');
    

    所以它会强制重定向到登录页面,后退按钮不会显示上一页

相关问题