编程语言
首页 > 编程语言> > PHP-使用Silex和Symfony 3配置LDAP身份验证

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

作者:互联网

我正在尝试使用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%’],而不是防火墙配置中的提供程序密钥.

尝试将您的配置更改为:

$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这个决定.我认为这更好.

标签:silex,ldap,symfony,php
来源: https://codeday.me/bug/20191027/1944478.html