注销时Symfony2 http缓存无效

时间:2016-04-18 17:51:38

标签: php symfony session caching

我遇到问题,在投入大量时间后我无法弄清楚如何解决。

我在我的应用程序中使用简单的http缓存,并在karnel上设置它,以便所有页面都有缓存。

这是我的缓存代码:

# myapp/src/MyWebBundle/EventListener/MyCacheListener.php 


namespace MyWebBundle\EventListener;

use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;


class MyCacheListener
{
private $authorizationChecker;
private $tokenStorage;

public function __construct(AuthorizationChecker $authorizationChecker, TokenStorage $tokenStorage)
{
    $this->tokenStorage = $tokenStorage;
    $this->authorizationChecker = $authorizationChecker;
}

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

    if($this->tokenStorage->getToken() != null && $this->authorizationChecker->isGranted("IS_AUTHENTICATED_ANONYMOUSLY")) {
        $response->headers->addCacheControlDirective('public', true);
        $response->headers->addCacheControlDirective('max-age', 86400);
        $response->headers->addCacheControlDirective('must-revalidate', true);
        //$response->headers->addCacheControlDirective('no-store', true);
    } else if ($this->tokenStorage->getToken() != null && $this->authorizationChecker->isGranted("IS_AUTHENTICATED_FULLY")) {
        $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;
}


}

然后在我的service.yml

my_web.event_listener.my_cache_listener:
      class: MyWebBundle\EventListener\MyCacheListener
      arguments: ["@security.authorization_checker", "@security.token_storage"]
      tags:
          - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }

我的退出成功处理程序

/**
 * logout success handler. removes the client login by client id
 *
 * @param Request        $request
 * @param Response       $response
 * @param TokenInterface $token
 */
public function onLogoutSuccess(Request $request)
{
    $token = $this->tokenStorage->getToken();
    $attributes = $token->getAttributes();
    $clientId = $attributes['device_id'];
    $dql = sprintf("delete My\UserBundle\Entity\ClientLogin login
        where login.device_id = '%s'", $clientId);
    $result = $this->em
        ->createQuery($dql)
        ->execute();
    $request->getSession()->invalidate();       


    @todo: for now redirecting to '/'. In future need get the target
    //from security.yml
    return $this->httpUtils->createRedirectResponse($request, '/');
}

现在我正在做什么用户登录没有缓存,如果没有那么所有内容都将被缓存。问题是如果我登录然后单击注销会话被销毁,但我仍然看到注销按钮,如果我再次点击该按钮,我收到错误,因为会话为空但再次尝试注销。但如果我手动刷新页面,那么我可以看到登录按钮没有注销。

如何在缓存所有内容的情况下解决已注销的问题?我现在不想使用网关缓存或FOSHttp缓存

0 个答案:

没有答案