Php 如何使用Silex框架实现自定义身份验证成功处理程序?

Php 如何使用Silex框架实现自定义身份验证成功处理程序?,php,symfony,authentication,silex,Php,Symfony,Authentication,Silex,我想在用户登录时跟踪一些数据(成功和失败),但我真的不知道怎么做 防火墙如下所示: $app->register(new Silex\Provider\SecurityServiceProvider(), array( 'security.firewalls' => array( 'secured' => array( 'pattern' => '^/', 'anonymous' => true

我想在用户登录时跟踪一些数据(成功和失败),但我真的不知道怎么做

防火墙如下所示:

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'secured' => array(
            'pattern' => '^/',
            'anonymous' => true,
            'logout' => true,
            'form' => array('login_path' => '/login',
                            'check_path' => '/login_check',
                            ),
            'users' => $app->share(function () use ($app) {
                return $app["dao.identifiant"];
            }),
        ),
    ),
));
我发现我必须注册以下服务:

$app['security.authentication.success_handler.secured'] = $app->share(function ($app) {
    ...
});
我还创建了一个实现AuthenticationSuccessHandlerInterface的自定义类:


添加身份验证成功处理程序

namespace Your\Namespace;

use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler as BaseDefaultAuthenticationSuccessHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class DefaultAuthenticationSuccessHandler extends BaseDefaultAuthenticationSuccessHandler
{
    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        // your actions

        return parent::onAuthenticationSuccess($request, $token);
    }
}
添加身份验证失败处理程序

namespace Your\Namespace;

use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler as BaseDefaultAuthenticationFailureHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class DefaultAuthenticationFailureHandler extends BaseDefaultAuthenticationFailureHandler
{
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        // your actions

        return parent::onAuthenticationFailure($request, $exception);
    }
}
并在申请中登记

$app['security.authentication.success_handler.secured'] = $app->share(function () use ($app) {
    $handler = new \Your\Namespace\DefaultAuthenticationSuccessHandler(
        $app['security.http_utils'],
        $app['security.firewalls']['secured']['form']
    );
    $handler->setProviderKey('secured');

    return $handler;
});

$app['security.authentication.failure_handler.secured'] = $app->share(function () use ($app) {
    return new \Your\Namespace\DefaultAuthenticationFailureHandler(
        $app,
        $app['security.http_utils'],
        $app['security.firewalls']['secured']['form'],
        $app['logger']
    );
});

非常感谢。我想做什么就做什么。我不得不编辑一些代码,但它仍然对我有很大帮助。:)它似乎是非常内存密集型的。我不得不将php.ini的内存限制提高到1.8g以上
$app['security.authentication.success_handler.secured'] = $app->share(function () use ($app) {
    $handler = new \Your\Namespace\DefaultAuthenticationSuccessHandler(
        $app['security.http_utils'],
        $app['security.firewalls']['secured']['form']
    );
    $handler->setProviderKey('secured');

    return $handler;
});

$app['security.authentication.failure_handler.secured'] = $app->share(function () use ($app) {
    return new \Your\Namespace\DefaultAuthenticationFailureHandler(
        $app,
        $app['security.http_utils'],
        $app['security.firewalls']['secured']['form'],
        $app['logger']
    );
});