LDAP,出自Lightweight Directory Access Protocol,它是“一个开放标准,基于Internet Protocol(IP)网络来访问和维护分发的目录信息服务”

LDAP作为一种集中式的用户信息宝库,包括其security roles,还能作为authentication验证服务,而被很多公司采用。这就是为何Symfony 2.8新增了一个全新的LDAP组件的原因,它提供了与Symfony Security安全组件的无缝整合。

在开启LDAP验证之前,定义一个新的服务,来配置连接设置,如host、port、LDAP版本等等。

1
2
3
4
5
# app/config/services.yml
services:
    app.ldap:
        class: Symfony\Component\Security\Ldap\Ldap
        arguments: [ "ldap.example.com" ]

然后你可以使用LDAP组件,既可把它作为一个user provider,也能把它当作firewall的authentication mechanism(验证机制之一)。以下配置是作为LDAP user provider时的选项:

1
2
3
4
5
6
7
8
9
10
11
12
13
# app/config/security.yml
security:
    # ...

    providers:
        app_users:
            ldap:
                service: app.ldap
                base_dn: dc=example,dc=com
                search_dn: CN=My User,OU=Users,DC=example,DC=com
                search_password: p455w0rd
                filter: (sAMAccountName={username})
                default_roles: ROLE_USER

当被用于验证机制时,你可以将它同一个login表单,或一个http basic mechanism,一起进行配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# app/config/security.yml
security:
    # ...

firewalls:
    api:
        provider:  app_users
        stateless: true
        pattern:   ^/api
        http_basic_ldap:
            service: app.ldap
            dn_string: "{username}@example"
    backend:
        provider: app_users
        pattern:  ^/admin
        logout:
            path:   logout
            target: login
        form_login_ldap:
            service: app.ldap
            dn_string: CN={username},OU=Users,DC=example,DC=com
            check_path: login_check
            login_path: login

本组件依赖于PHP的LDAP extension扩展,确保在使用组件之前,做好相应的php配置,并开启该扩展。