Silex Security success_handler

时间:2013-06-24 18:17:00

标签: security silex

如何为表单身份验证提供程序设置success_handler(和failure_handler)?

Silex用这个配置忽略了我:

<?php

use WebFactory\Security\UserProvider;

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'dev' => array(
            'pattern' => '^/(_(profiler|wdt)|css|images|js)/',
            'security' => false
        ),
        'default' => array(
            'pattern' => '^/.*$',
            'anonymous' => true,
            'form' => array(
                'login_path' => '/login',
                'check_path' => '/login_check',
                'success_handler' => 'authentication_handler', //<-- here
                'failure_handler' => 'authentication_handler', //<-- here
            ),
            'logout' => array('logout_path' => '/logout'),
            'users' => $app->share(function () use ($app) {
                        return new UserProvider($app['db']);
                    }),
        ),
    ),
    'security.access_rules' => array(
        array('^/login', 'IS_AUTHENTICATED_ANONYMOUSLY'),
        array('^/private$', 'ROLE_ADMIN'),
    ),
    'security.role_hierarchy' => array(
        'ROLE_SIMPLE_USER' => array('ROLE_USER'),
        'ROLE_ASSOCIATE' => array('ROLE_USER'),
    )
));

这是我的习惯(从未被调用过)

$app['authentication_handler'] = $app->share(function ($app) {
            return new \WebFactory\Security\AuthenticationHandler($app['url_generator']);
        });

这是一个错误?

1 个答案:

答案 0 :(得分:7)

设置成功和失败处理程序的方法是定义名为security.authentication.success_handler.$namesecurity.authentication.failure_handler.$name的服务,其中$name是防火墙的名称。

例如:

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'foo' => ...,
    ),
));

$app['security.authentication.success_handler.foo'] = $app->share(function ($app) {
    return new Your\Own\SuccessHandler();
});

然后,安全服务提供程序将按惯例检测处理程序。