Symfony2-注销并清除缓存+防止后退按钮 [英] Symfony2 - Logout and clear cache + prevent back button

查看:113
本文介绍了Symfony2-注销并清除缓存+防止后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在用户注销时终止浏览器缓存. 我实现了LogoutSuccessHandlerInterface来扩展onLogoutSuccess方法. 没有错误,但是注销后,可以在浏览器中按返回按钮,然后看到配置文件页面=>如果刷新此页面,则会自动重定向,因此可以正确注销.

I try to kill browser cache when user logout. I implement the LogoutSuccessHandlerInterface to extends the onLogoutSuccess method. There is no error but when I logout, I can press back button in browser and I see my profil page => If I refresh this page, I am automatically redirected, so I am correctly logged out.

security.yml

logout:
    path:   /logout
    target: /
    invalidate_session: true
    success_handler: project_user.handler.logout_handler


services.yml

project_user.handler.logout_handler:
    class:  Project\UserBundle\Handler\LogoutHandler


Project/UserBundle/Handler/LogoutHandler.php

<?php
namespace Project\UserBundle\Handler;

use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

class LogoutHandler implements LogoutSuccessHandlerInterface
{
  public function onLogoutSuccess( Request $request )
  {
    $response =  new RedirectResponse( '/' );

    $response->headers->addCacheControlDirective( 'no-cache', true );
    $response->headers->addCacheControlDirective( 'max-age', 0 );
    $response->headers->addCacheControlDirective( 'must-revalidate', true );
    $response->headers->addCacheControlDirective( 'no-store', true );

    return $response;
  }
}

我尝试使用此解决方案,效果很好,但是这种方法对于每个请求都会被调用(每页很多次),并导致速度降低. 请帮忙!

I try with this solution and that works perfectly, but this method is called for each requests (many time for each pages) and caused slowdowns. Please help!

thx

推荐答案

尝试一下,为我工作.

<?php 

namespace YourBundle;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;

class KernelSubscriber implements EventSubscriberInterface {

    public static function getSubscribedEvents() {
        return array(
            KernelEvents::RESPONSE => array(
                array('clearBrowserCache', 434255),
            ),
        );
    }

    public function clearBrowserCache(FilterResponseEvent $event) {
        $response = $event->getResponse();

        $response->headers->addCacheControlDirective('no-cache', true);
        $response->headers->addCacheControlDirective('max-age', 0);
        $response->headers->addCacheControlDirective('must-revalidate', true);
        $response->headers->addCacheControlDirective('no-store', true);        
    }

}

services.yml

services.yml

kernel_subscriber:
    class: YourBundle\KernelSubscriber
    tags:
        - { name: kernel.event_subscriber }

这篇关于Symfony2-注销并清除缓存+防止后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆