Symfony - 如何在身份验证失败侦听器中获取用户名和 IP 地址? [英] Symfony - How to get username and IP address in authentication failure listener?

查看:30
本文介绍了Symfony - 如何在身份验证失败侦听器中获取用户名和 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要:

  • 用户名
  • IP 地址

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

for each users that tries to log in to my website. There is no problem to get that information if user logged successfully but I don't know how to get IP address for failure attempts.

她是我的代码:

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 注入监听器并从中获取 Request.您可以从 getClientIp()

Inject RequestStack into listener and get Request from it. You can get IP from getClientIp()

/**
 * @var RequestStack
 */
protected $requestStack;

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

这篇关于Symfony - 如何在身份验证失败侦听器中获取用户名和 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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