2016-01-22 49 views
3

我嘗試使用Symfony(3.0.1)提供的LDAP組件在我的Silex(1.3.5)應用程序中配置LDAP身份驗證。 這裏是我當前的配置:使用Silex和Symfony 3配置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

有什麼做的,使與Silex的LDAP組件的工作?或者我錯過了一些配置?我沒有找到該組件的任何文檔...

回答

4

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, 
     ], 
    ], 
]); 

UPDATE:或者更好地利用這個Symfony LDAP auth bind with username and password決定。我認爲這樣更好。

+0

謝謝,它現在正在工作。 –