gpt4 book ai didi

php - Symfony2 注销事件监听器问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:07:51 24 4
gpt4 key购买 nike

我正在努力将审计日志记录添加到 Symfony2 项目,该项目将所有页面加载和发布请求记录到自定义审计表中。该项目使用 Symfony2 的默认注销路由(访问/logout),它会破坏 session ,然后重定向到/login 路由。

为 onKernelRequest 设置了一个事件监听器,然后将正确的数据写入表中。在 security.yml 文件中,我为注销路由列出了以下内容。

security:
firewalls:
main:
logout:
path: /logout
target: /login

除注销事件外,所有页面的审核日志记录都正常工作。注销后,我尝试访问探查器,然后从侧边栏的“最后 10 个”选项中选择“/注销”操作。单击“事件”时,它会列出 kernel.request 的默认 Symfony 事件,例如 DebugHandler 和 ProfileListener,但我的自定义回调显示在“未调用的监听器”选项卡下。

有一个 success_handler 可以添加到 security.yml 文件中,但是我需要一个可以在 session 被销毁之前运行我的事件监听器的方法。有没有办法让现有的监听器在 Symfony 执行注销操作之前也记录注销事件?

编辑

<?php

// src/AuditBundle/EventListener/AuditListener.php
namespace AuditBundle\EventListener;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\RequestStack;
use AuditBundle\Entity\AuditLog;

class AuditListener
{
protected $requestStack;
protected $em;
protected $tokenStorage;
protected $authorizationChecker;

public function __construct(RequestStack $requestStack, \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage $tokenStorage, $authorizationChecker, \Doctrine\ORM\EntityManager $em = NULL)
{
$this->requestStack = $requestStack;
$this->em = $em;
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $authorizationChecker;
}

public function onKernelRequest(GetResponseEvent $response)
{
$request = $response->getRequest();
if ( strpos($request->getRequestUri(), 'fonts') !== false)
return;
if ( strpos($request->getRequestUri(), 'css') !== false)
return;
if ( strpos($request->getRequestUri(), '_wdt') !== false)
return;
if ( strpos($request->getRequestUri(), 'js') !== false)
return;

if ( strpos($request->getRequestUri(), '_profiler') !== false)
return;

$this->log('Request', $request);
}

public function postUpdate(\Doctrine\ORM\Event\LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$this->em = $args->getEntityManager();
$className = $this->em->getClassMetadata(get_class($entity))->getName();
if ($entity instanceof \AuditBundle\Entity\AuditLog)
return;
$this->log($className.' Updated', $this->requestStack->getCurrentRequest(), $entity);
}

public function postPersist(\Doctrine\ORM\Event\LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$this->em = $args->getEntityManager();
$className = $this->em->getClassMetadata(get_class($entity))->getName();
if ($entity instanceof \AuditBundle\Entity\AuditLog)
return;
$this->log($className.' Created', $this->requestStack->getCurrentRequest(), $entity);
}

public function postDelete(\Doctrine\ORM\Event\LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$this->em = $args->getEntityManager();
$className = $this->em->getClassMetadata(get_class($entity))->getName();
if ($entity instanceof \AuditBundle\Entity\AuditLog)
return;
$this->log($className.' Deleted', $this->requestStack->getCurrentRequest());
}

protected function log($message, $request, $entity = NULL)
{
$log = new AuditLog();
$log->setType($request->getRealMethod());
if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY'))
{
$log->setUser($this->tokenStorage->getToken()->getUser());
}
if ($entity)
{
$log->setEntityId($entity->getId());
}

$log->setUriString($request->getRequestUri());
$log->setMessage($message);
$log->setDatetime(new \DateTime());
$log->setIp($request->getClientIp());
$log->setBrowser(isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '');
$this->em->persist($log);
$this->em->flush();
}
}

服务.yml

services:
audits.audit_listener:
class: AuditBundle\EventListener\AuditListener
arguments: [@request_stack, @security.token_storage, @security.authorization_checker, @doctrine.orm.entity_manager]
tags:
- { name: kernel.event_listener, event: kernel.request }

最佳答案

挂接到 Symfony 的注销事件的最好方法是实现 LogoutHandlerInterface像这样,

事件监听器:

<?php

namespace AppBundle\EventListener;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;

class MyListener implements LogoutHandlerInterface
{
public function logout(Request $request, Response $response, TokenInterface $token)
{
// Your handling here
}
}

配置:

services:
appbundle_mylistener:
class: AppBundle\EventListener\MyListener

security:
firewalls:
main:
logout:
handlers: [appbundle_mylistener]

因此,您真正需要做的就是使用 logout 方法从您的 AuditListener 实现 LogoutHandlerInterface,然后注册 handlers 配置中的参数

关于php - Symfony2 注销事件监听器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38593068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com