简体   繁体   English

Symfony 2事件侦听器-身份验证失败/防火墙侦听器

[英]Symfony 2 event listener - on authentication fail / firewall listener

So I can perform a simple request listener: 因此,我可以执行一个简单的请求侦听器:

public function onKernelRequest(GetResponseEvent $event)
{
    $request = $event->getRequest();

        //etc.

And in services.yml: 并在services.yml中:

  listener.my_request_listener:
    arguments: 
      - "@translator"
    class: MyBundle\EventListener\RequestListener
    tags: 
      - 
        event: kernel.request
        method: onKernelRequest
        name: kernel.event_listener
      - 
        event: kernel.response
        method: onKernelResponse
        name: kernel.event_listener

What I want to do now is detect if authentication has failed (using Symfony2's in-built security component). 我现在要做的是检测身份验证是否失败(使用Symfony2的内置安全组件)。 I have all the security setup, but I need to detect when security fails. 我已完成所有安全设置,但是我需要检测安全性何时失败。 Something like this: 像这样:

public function onKernelAuthFail(GetAuthenticationFailEvent $event) {

    //authentication failed, so action can be performed here before it redirects

}

I've seen this: http://api.symfony.com/2.3/Symfony/Component/Security/Http/Firewall/ListenerInterface.html and talk of 'firewall' listeners, but I just can't understand how to do it. 我已经看到了这一点: http : //api.symfony.com/2.3/Symfony/Component/Security/Http/Firewall/ListenerInterface.html并谈到了“防火墙”侦听器,但我只是不明白该怎么做。 I bet it's only a line of code or something. 我敢打赌,这只是一行代码之类的东西。

Take a look at the DefaultAuthenticationFailureHandler class. 看一下DefaultAuthenticationFailureHandler类。 It should be under Symfony\\Component\\Security\\Http\\Authentication . 它应该在Symfony\\Component\\Security\\Http\\Authentication This is the default listener that Symfony uses when authentication fails. 这是身份验证失败时Symfony使用的默认侦听器。

But you can write your own and use it by adding it to your security.yml like this: 但是您可以自己编写并使用它,方法是将其添加到security.yml如下所示:

firewalls:
    firewall_name:
        form_login:
            failure_handler: your.service.id

Here's a complete reference to the security.yml configuration: http://symfony.com/doc/current/reference/configuration/security.html 这是对security.yml配置的完整参考: http : //symfony.com/doc/current/reference/configuration/security.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM