dongliao1949 2016-01-25 10:00
浏览 70
已采纳

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

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条回答 默认 最新

  • 普通网友 2016-01-25 12:02
    关注

    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']
        );
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀