How to implement a custom authentication success h

2019-05-16 16:34发布

I'd like to track some data when users are logging in (success and failure) but I don't really know how to do it.

The firewall looks like this :

$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"];
            }),
        ),
    ),
));

I found that I have to register a service like :

$app['security.authentication.success_handler.secured'] = $app->share(function ($app) {
    ...
});

And I also created a custom class implementing the AuthenticationSuccessHandlerInterface :

<?php

namespace myproject\Authentication;

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

class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
{

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

I have a class called Connection in which I have a function to record some information in my database (like the user ID, date & time of the connection, if he failed or succeded to log in, etc.). How can I manage to call this function whenever a user tries to log in ?

Thanks !

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-05-16 17:33

Add authentication success handler

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);
    }
}

Add authentication failure handler

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);
    }
}

and register them in application

$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']
    );
});
查看更多
登录 后发表回答