如何扩展 LdapUserProvider 并在 Symfony 中使用自定义 LDAP 用户提供程序?

How to extend LdapUserProvider and use a custom LDAP user provider in Symfony?

我正在努力更换 LdapUserProvider。

我创建了我自己的提供程序(App\Security\MyLdapUserProvider 基于 LdapUserProvider 但检索了更多信息)和我自己的 UserInterface (App\Security\MyUser),具有更多属性来存储数据。

最后我想检索组和用户的显示名称。

这是我的配置:

services.yaml:

# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
Symfony\Component\Ldap\Ldap:
    arguments: ['@Symfony\Component\Ldap\Adapter\ExtLdap\Adapter']
Symfony\Component\Ldap\Adapter\ExtLdap\Adapter:
    arguments:
        - host: 10.106.1.1
          port: 389
          #encryption: tls
          options:
              protocol_version: 3
              referrals: false

security.yaml:

providers:
    #in_memory: { memory: ~ }
    my_ldap:
        ldap:
            service: Symfony\Component\Ldap\Ldap
            base_dn: "dc=XXXXXX,dc=com"
            search_dn: "CN=XXXXXXXXXX,OU=LDAP,OU=Services Accounts,OU=Administration,DC=XXXXXXXXX,DC=com"
            search_password: "ergergergergerg"
            default_roles: ROLE_USER
            filter: "({uid_key}={username})"
            uid_key: samAccountName
            #password_attribute: displayName
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        pattern: ^/
        security: true
        anonymous: true
        provider: my_ldap
        form_login_ldap:
            login_path: /login
            check_path: /login
            service: Symfony\Component\Ldap\Ldap
            dn_string: 'dc=XXXXXX,dc=com'
            query_string: '(samAccountName={username})'
        logout:
            path:   /logout
            target: /

我在哪里可以告诉安全提供者使用我自己的 ldap 提供者而不是默认提供者?

Symfony 流程对我来说还是有点复杂所以如果有人能花时间解释..

Symfony 文档是在 CustomUserProvider > Ldap 配置 > CustomeUSerProvider 之间重定向的无限循环...

如文档章节 Creating A Custom User Provider 中所述,您需要将用户提供者添加为 security.providers 下的新键并将其配置为 id

id 是您的自定义用户提供程序服务的名称,在最新版本的 symfony 中,它等于 FQCN。

# security.yaml

security:
  providers:
    # the name of your user provider can be anything
    my_ldap_provider:
      id: 'App\Security\MyLdapUserProvider'

然后您可以将此提供程序用于这样的防火墙之一:

security:
  # [..]
  firewalls:
    main:
      pattern: '^/'
      provider: 'my_ldap_provider'

Symfony 的 LdapUserProvider 看起来像这样:

class LdapUserProvider implements UserProviderInterface
{
    private $ldap;
    private $baseDn;
    private $searchDn;
    private $searchPassword;
    private $defaultRoles;
    private $uidKey;
    private $defaultSearch;
    private $passwordAttribute;
    private $extraFields;

    public function __construct(
      LdapInterface $ldap,
      string $baseDn,
      string $searchDn = null,
      string $searchPassword = null,
      array $defaultRoles = [],
      string $uidKey = null,
      string $filter = null,
      string $passwordAttribute = null,
      array $extraFields = []
    )
    {

为了创建正确扩展 LdapUserProviderMyLdapUserProvider 服务,您需要这样的 service-definition:

# services.yaml

services:
  App\Security\MyLdapUserProvider:
    arguments:
      $adminEmail: '%admin_email%'
      $ldap: '@Symfony\Component\Ldap\Ldap'
      $baseDn: 'dc=XXXXXX,dc=com'
      $searchDn: 'CN=XXXXXXXXXX,OU=LDAP,OU=Services Accounts,OU=Administration,DC=XXXXXXXXX,DC=com'
      $searchPassword: 'ergergergergerg'
      $defaultRoles: ['ROLE_USER']
      $filter: '({uid_key}={username})'
      $uidKey: 'samAccountName'