我遵循此处的指南并实施了基本的身份验证系统: https://symfony.com/doc/current/security.html
但是,我想将系统日志添加到我的应用程序中。确切地说,我想要:
我知道我可以将日志放入我的SecurityController中,就像这样:
public function login(AuthenticationUtils $authenticationUtils, Request $request, EntityManagerInterface $em): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
$log = new Logs();
$em->persist($log);
$log->setAction('auth')
->setDate(new DateTime())
->setIp($request->getClientIp());
$em->flush();
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
但是它只给我有关某人在登录页面上的信息。如何修改或添加以获得其他信息?
答案 0 :(得分:1)
我认为这将解决您的问题。
您应该通过Symfony事件来做到这一点。失败/成功的登录尝试后,将触发以下两个事件:combined_key = "rateRequestPayments-0-amount"
key, index, subkey = combined_key.split("-")
del request_body[key][int(index)][subkey]
和security.authentication.success
。
我将举例说明成功,您可以将其应用于失败:
将此添加到您的security.authentication.failure
config/service.yml
然后您可以创建侦听器并在其中进行日志记录过程
App\EventListener\SuccessLoginListener:
tags:
- { name: 'kernel.event_listener', event: 'security.authentication.success'}