2013-05-23 22 views
7

Tôi cố gắng viết nhà cung cấp xác thực tùy chỉnh để xác thực LDAP bằng tài liệu silex - Defining a custom Authentication Provider.Nhà cung cấp xác thực tùy chỉnh trong ứng dụng silex

Nhưng nếu tôi nhìn vào $app['security.authentication_providers'] thì có hai nhà cung cấp. Một tài khoản mà tôi đã xác định App\LdapAuthenticationProvider và một Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider

Và khi tôi cố gắng cho phép người dùng, tôi gặp lỗi vì có cuộc gọi App\LdapUserProvider::loadUserByUsername() từ lớp DaoAuthenticationProvider.

Nếu tôi chỉ có một nhà cung cấp trong $app['security.authentication_providers'] Tôi nghĩ tôi không nên gặp lỗi vì nhà cung cấp LDAP của tôi không gọi loadUserByUsername.

Đây là bãi chứa của $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) 

Vì vậy, không ai biết lý do tại sao có thêm nhà cung cấp và làm thế nào tôi có thể thoát khỏi nó?

Có mã cho bootstraping application, LdapAuthenticationListenerLdapAuthenticationProvider.

Trả lời

2

Sự cố được giải quyết.

Tôi vừa mới mở rộng lớp LdapAuthenticationListener tôi với symfony2 UsernamePasswordFormAuthenticationListener và thay đổi bootstarp như thế này:

$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' 
     ); 
    } 

tôi cần xác thực người nghe tùy chỉnh để ghi đè mã thông báo trong phương thức xác thực và cung cấp dịch vụ xác thực người dùng lấy từ nhà cung cấp người dùng bằng tên truy cập và mật khẩu $this->userProvider->loadUserByUsernameAndPassword($usernam, $password)

+0

Tuy nhiên, vẫn không thể hiểu tại sao '' $ app ['security.authentication_providers'] '' có hai nhà cung cấp. – vansanblch

+0

bạn có thể đăng mã cuối cùng mà bạn đã thực hiện không? – heapOverflow

+0

Thật không may là tôi không thể. Dự án này được rút ra ngay bây giờ và tôi đã không truy cập vào nó. Nhưng ý tưởng chính là viết LdapAuthenticationListener chính xác mà tôi đã lưu trữ bằng cách mở rộng nó từ UsernamePasswordFormAuthenticationListener. Và có một mã trong một bootstrap để đăng ký nhà máy mới để xử lý xác thực ldap. Ngoài ra hãy xem mã tại pastebin, linku có thể được tìm thấy trong bình luận đầu tiên. Tôi nghĩ rằng tôi đã không thay đổi mã trong các lớp này quá nhiều. – vansanblch

Các vấn đề liên quan