【问题标题】:Custom authentication provider in silex applicationsilex 应用程序中的自定义身份验证提供程序
【发布时间】:2013-05-18 14:41:46
【问题描述】:

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

但是如果我查看$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的代码。

【问题讨论】:

    标签: authentication silex


    【解决方案1】:

    问题解决了。

    我刚刚用 symfony2 UsernamePasswordFormAuthenticationListener 扩展了我的 LdapAuthenticationListener 类,并像这样更改 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)从用户提供程序检索用户

    【讨论】:

    • 但是,仍然不明白为什么$app['security.authentication_providers'] 有两个提供者。
    • 不幸的是我不能。这个项目现在被撤回了,我无权访问它。但主要思想是编写正确的 LdapAuthenticationListener,我通过从 UsernamePasswordFormAuthenticationListener 扩展它来归档它。并且在引导程序中有一个代码来注册新工厂来处理 ldap-authentication。还可以查看 pastebin 中的代码,linku 可以在第一条评论中找到。我想我并没有过多地更改这些类中的代码。
    猜你喜欢
    • 2012-04-05
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2013-11-15
    相关资源
    最近更新 更多