gpt4 book ai didi

php - Symfony - 如何在身份验证失败监听器中获取用户名和 IP 地址?

转载 作者:可可西里 更新时间:2023-11-01 14:00:47 26 4
gpt4 key购买 nike

我需要得到:

  • 用户名
  • IP 地址

对于每个尝试登录我的网站的用户。如果用户登录成功,获取该信息没有问题,但我不知道如何获取失败尝试的 IP 地址。

她是我的代码:

app.listener.interactive_login_listener:
class: AppBundle\EventListener\LogonListener
arguments: ['@security.token_storage', '@doctrine.orm.entity_manager', '@security.authorization_checker', '@router', '@manager.settings']
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: onAuthenticationSuccess }

# authentication failure event listener
app.listener.authentication_failure_event_listener:
class: AppBundle\EventListener\LogonListener
arguments: ['@security.token_storage', '@doctrine.orm.entity_manager', '@security.authorization_checker', '@router', '@manager.settings']
tags:
- { name: kernel.event_listener, event: security.authentication.failure, method: onAuthenticationFailure }

听众:

class LogonListener implements EventSubscriberInterface
{

private $_tokenStorage;
private $_em;
private $_authorizationChecker;
private $_router;
private $_settings;

public function __construct(
TokenStorage $tokenStorage,
EntityManager $em,
AuthorizationCheckerInterface $authorizationChecker,
Router $router,
$settings)
{
$this->_tokenStorage = $tokenStorage;
$this->_em = $em;
$this->_authorizationChecker = $authorizationChecker;
$this->_router = $router;
$this->_settings = $settings;
}

/**
* getSubscribedEvents
*
* @return array
*/
public static function getSubscribedEvents()
{
return array(
AuthenticationEvents::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccess',
AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
);
}

/**
* onAuthenticationSuccess
*
* @param InteractiveLoginEvent $event
*/
public function onAuthenticationSuccess(InteractiveLoginEvent $event)
{
var_dump( $event->getAuthenticationToken()->getUser() );
var_dump( $event->getRequest()->getClientIp() );
die();
}

public function onAuthenticationFailure(AuthenticationFailureEvent $event)
{
$userName = $event->getAuthenticationToken()->getUsername();
$user = $this->_em->getRepository('AppBundle:User')->findOneByUsername($userName);

var_dump( $user );
// how to get IP?
die();
}

最佳答案

RequestStack 注入(inject)监听器并从中获取 Request。您可以从 getClientIp()

获取 IP
/**
* @var RequestStack
*/
protected $requestStack;

/**
* @return string
*/
protected function resolveClientIp()
{
return $this->requestStack->getMasterRequest()->getClientIp();
}

关于php - Symfony - 如何在身份验证失败监听器中获取用户名和 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38693147/

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