在symfony2.1中注册后触发认证成功事件



注册后,我确保用户登录如下:

$token = new UsernamePasswordToken($user, null, 'secured_area', $user->getRoles());
$this->get('security.context')->setToken($token);

,并希望这也会触发我的成功处理程序:

$this->get('event_dispatcher')->dispatch(
    AuthenticationEvents::AUTHENTICATION_SUCCESS,
    new AuthenticationEvent($token)
);

处理程序在安全配置中的标准表单登录上设置,例如

firewalls:
    secured_area:
        form_login:
            success_handler: authentication_handler

成功处理程序在登录时被触发,但在注册后不会被触发。

这只是一个配置问题吗?或者,所有这些都可以通过一个单一的事件来实现吗?

我认为您必须验证用户才能触发事件:

$token = $this->get('my.bundle.authentication.manager')->authenticate(new UsernamePasswordToken($user, null, 'secured_area', $user->getRoles()));
$this->get('security.context')->setToken($token);

my.bundle.authentication.manager将是security.authentication.manager的别名,它可以像这样注册(XML):

<service id="my.bundle.authentication.manager" alias="security.authentication.manager" />

最新更新