使用 Silex 和 Symfony 3 配置 LDAP 身份验证

Configuring LDAP authentication with Silex and Symfony 3

我正在尝试使用 Symfony (3.0.1) 提供的 LDAP 组件在我的 Silex (1.3.5) 应用程序中配置 LDAP 身份验证。 这是我当前的配置:

$app['ldap'] = function () {
    return new Symfony\Component\Ldap\LdapClient('ldap.example.com');
};

$app->register(new SecurityServiceProvider(), [
    'security.providers' => [
        'users' => [
            'ldap' => [
                'service' => 'ldap',
                'base_dn' => 'dc=example,dc=com',
                'search_dn' => 'CN={username},OU=DEV,DC=example,DC=com',
            ],
        ],
    ],
    'security.firewalls' => [
        'stats' => [
            'pattern' => '^/',
            'provider' => 'users',
            'http' => true,
            'stateless' => true,
            'http_basic_ldap' => [
                'service' => 'ldap',
                'dn_string' => '{username}@example.com',
            ],
        ],
    ],
]);

但是使用上面的配置,我得到以下异常:

Fatal error: Uncaught exception 'LogicException' with message 'The "provider" authentication entry is not registered.' in /var/www/my-site/vendor/silex/silex/src/Silex/Provider/SecurityServiceProvider.php:243 Stack trace: #0 /var/www/my-site/vendor/pimple/pimple/lib/Pimple.php(126): Silex\Provider\SecurityServiceProvider->Silex\Provider{closure}(Object(Silex\Application)) #1 /var/www/my-site/vendor/pimple/pimple/lib/Pimple.php(83): Silex\Application::{closure}(Object(Silex\Application)) #2 /var/www/my-site/vendor/silex/silex/src/Silex/Provider/SecurityServiceProvider.php(150): Pimple->offsetGet('security.firewa...') #3 /var/www/my-site/vendor/pimple/pimple/lib/Pimple.php(126): Silex\Provider\SecurityServiceProvider->Silex\Provider{closure}(Object(Silex\Application)) #4 /var/www/my-site/vendor/pimple/pimple/lib/Pimple.php(83): Silex\Application::{closure}(Object(Silex\Application)) #5 /var/www/my-site/vendor/silex/silex/src/Silex/Provider/SecurityServiceProvider.php(584): in /var/www/my-site/vendor/silex/silex/src/Silex/Provider/SecurityServiceProvider.php on line 243

有什么办法可以使 LDAP 组件与 Silex 一起工作吗?还是我缺少一些配置?我没有找到该组件的任何文档...

Silex 安全配置以其他方式工作。
用户提供商定义为 $app['security.user_provider.%firewall_name%'],而不是防火墙配置中的 provider 键。

尝试将您的配置更改为:

$app['ldap'] = $app->share(function() {
    return new Symfony\Component\Ldap\LdapClient('ldap.example.com');
});

$app['security.user_provider.stats'] = $app->share(function($app) {
    return new \Symfony\Component\Security\Core\User\LdapUserProvider(
        $app['ldap'],
        'dc=example,dc=com',
        null,
        null,
        ['ROLE_USER'],
        'CN'
    );
});

$app['security.authentication_provider.stats.dao'] = function () use ($app) {
    return new \Symfony\Component\Security\Core\Authentication\Provider\LdapBindAuthenticationProvider(
        $app['security.user_provider.stats'],
        $app['security.user_checker'],
        'stats',
        $app['ldap'],
        'CN={username},OU=DEV,DC=example,DC=com',
        $app['security.hide_user_not_found']
    );
};

$app->register(new Silex\Provider\SecurityServiceProvider(), [
    'security.firewalls' => [
        'stats' => [
            'pattern' => '^/',
            'http' => true,
            'stateless' => true,
        ],
    ],
]);

更新: 或者更好地使用这个决定 Symfony LDAP auth bind with username and password。我认为这样更好。