onAuthenticationSuccess为我提供事件作为参数

时间:2013-09-30 15:11:53

标签: php symfony

我在身份验证成功时有以下处理程序:

app_auth_success_handler:
        class: App\UserBundle\Security\User\Handler\LoginAuthSuccessHandler
        public: true
        arguments: ['@router', "@service_container"]
        tags:
             - { name: kernel.event_listener, event: security.authentication.success, method: onAuthenticationSuccess }

class LoginAuthSuccessHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
    private $router;
    private $container;

    /**
    * Constructor
    * @param RouterInterface   $router
    */
    public function __construct(RouterInterface $router, $container)
    {
        $this->router = $router;
        $this->container = $container;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {

但是,在我对用户进行身份验证后,它给了我这个错误:

Catchable fatal error: Argument 1 passed to App\UserBundle\Security\User\Handler\LoginAuthSuccessHandler::onAuthenticationSuccess() must be an instance of Symfony\Component\HttpFoundation\Request, instance of Symfony\Component\Security\Core\Event\AuthenticationEvent given in /Users/John/Sites/App/src/Shopious/UserBundle/Security/User/Handler/LoginAuthSuccessHandler.php on line <i>31</i></th></tr>

无法弄清楚为什么会这样..

1 个答案:

答案 0 :(得分:2)

您误将security.authentication.success事件与您可以在防火墙配置中定义的success_handler混淆。

success_handler的服务可以是实施AuthenticationSuccessHandlerInterface的任何服务。

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class LoginAuthSuccessHandler implements AuthenticationSuccessHandlerInterface
{
    public function onAuthenticationSuccess(Request $request, TokenInterface $token) {}
}

不需要将其标记为事件侦听器:

app_auth_success_handler:
    class: App\UserBundle\Security\User\Handler\LoginAuthSuccessHandler
    public: true
    arguments: ['@router', "@service_container"]

而是将服务名称传递给防火墙配置:

security:
    firewalls:
        main:
            form_login:
                success_handler: app_auth_success_handler