在silex应用程序中自定义身份验证提

时间:2013-05-23 09:42:39

标签: authentication silex

我尝试使用silex文档编写自定义身份验证提供程序以进行LDAP身份验证 - Defining a custom Authentication Provider

但如果我调查$app['security.authentication_providers'],有两个提供者。我定义了一个App\LdapAuthenticationProvider和一个Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider

当我尝试授权用户时,我收到错误,因为DaoAuthenticationProvider类调用了App\LdapUserProvider::loadUserByUsername()

如果我在$app['security.authentication_providers']中只有一个提供者,我认为我不应该收到错误,因为我的LDAP提供者不会调用loadUserByUsername。

这是$app['security.authentication_providers']的转储     

array (size=2)
  0 => object(App\LdapAuthenticationProvider)[194]
    private 'userProvider' => 
      object(App\LdapUserProvider)[176]
        private 'ldap' => resource(57, ldap link)
        private 'defaultRoles' => 
          array (size=1)
          ...
    private 'providerKey' => string 'default' (length=7)
  1 => object(Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider)[195]
    private 'encoderFactory' => 
      object(Symfony\Component\Security\Core\Encoder\EncoderFactory)[197]
        private 'encoders' => 
          array (size=1)
          ...
    private 'userProvider' => 
      object(App\LdapUserProvider)[176]
        private 'ldap' => resource(57, ldap link)
        private 'defaultRoles' => 
          array (size=1)
          ...
    private 'hideUserNotFoundExceptions' (Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider) => boolean true
    private 'userChecker' (Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider) => object(Symfony\Component\Security\Core\User\UserChecker)[196]
    private 'providerKey' (Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider) => string 'default' (length=7)

那么,有没有人知道为什么有额外的提供者,我怎么能摆脱它呢?

bootstraping applicationLdapAuthenticationListenerLdapAuthenticationProvider的代码。

1 个答案:

答案 0 :(得分:2)

问题解决了。

我刚刚使用symfony2 LdapAuthenticationListener扩展了我的UsernamePasswordFormAuthenticationListener课程并更改了bootstarp:

$app['security.authentication_listener.factory.ldap'] = $app->protect(
    function ($name, $options) use ($app) {
        $app['security.authentication_provider.'.$name.'.ldap'] = $app->share(
            function () use ($app) {
                return new LdapAuthenticationProvider(
                    $app['security.user_provider.default'],
                    'ldap'
                );
            }
        );

        $app['security.authentication_listener.'.$name.'.ldap'] = $app->share(
            function () use ($app, $name, $options) {
                $app['security.authentication.success_handler.'.$name] =
                    $app['security.authentication.success_handler._proto']($name, $options);
                $app['security.authentication.failure_handler.'.$name] =
                    $app['security.authentication.failure_handler._proto']($name, $options);

                return new LdapAuthenticationListener(
                    $app['security'],
                    $app['security.authentication_manager'],
                    $app['security.session_strategy'],
                    $app['security.http_utils'],
                    $name,
                    $app['security.authentication.success_handler.'.$name],
                    $app['security.authentication.failure_handler.'.$name],
                    array_merge(
                        array(
                            'check_path' => '/admin/login_check',
                            'login_path' => '/login',
                        ),
                        $options
                    ),
                    $app['logger'],
                    $app['dispatcher'],
                    null
                );
            }
        );

        return array(
            'security.authentication_provider.'.$name.'.ldap',
            'security.authentication_listener.'.$name.'.ldap',
            null,
            'pre_auth'
        );
    }

我需要自定义身份验证侦听器来覆盖身份验证方法中的令牌,身份验证提供程序通过用户名和密码$this->userProvider->loadUserByUsernameAndPassword($usernam, $password)

从用户提供程序中检索用户