Silex/Symfony安全:如何对用户登录执行额外的检查


Silex/Symfony Security: How to perform additional checks for user login?

我使用的是Silex Security,防火墙和安全设备是这样配置的:

$app->register(new Silex'Provider'SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'admin' => array(
            'pattern' => '^/secured/',
            'form' => array(
                'login_path' => '/login',
                'check_path' => '/secured/login_check'
            ),
            'users' => $app->share(function() use ($app) {
                return new CustomUserProvider($app["db"]);
            }),
            'logout' => array('logout_path' => '/secured/logout'),
            'switch_user' => array('parameter' => '_cambiar_usuario', 'role' => 'ROLE_ALLOWED_TO_SWITCH')
        )
    )
));

所有工作正常,但现在我想执行额外的检查用户,在这种情况下,我想检查用户的电子邮件是否被验证(它只是检查数据库字段是否为真或假),更多的用户-密码检查。如果邮件验证尚未完成,我想使用自定义消息使验证失败。

我自己解决了。我必须扩展Symfony的UserChecker类并覆盖checkPreAuth方法来添加我的额外检查。

之后,我将默认的用户检查器替换为我的:

$app->register(new SecurityServiceProvider(), array(
    'security.firewalls' => array(
         //my firewalls
    )
));
$app['security.user_checker'] = $app->share(function ($app) {
    return new CustomUserChecker();
});