1

我将 FR3DLdapBundle 与 FOSUserBundle 一起使用。

Symfony 版本 3.2.6 FR3DLdapBundle 版本 3

配置.yml

fos_user:
    db_driver: orm # other valid values are 'mongodb' and 'couchdb'
    firewall_name: main
    user_class: AppBundle\Entity\User
    from_email:
        address: "%mailer_user%"
        sender_name: "%mailer_user%"

fr3d_ldap:
    driver:
        host: ldap.forumsys.com
        port: 389
        username: cn=read-only-admin,dc=example,dc=com
        password: password
        bindRequiresDn: true
    user:
        baseDn: dc=example,dc=com
        filter: (&(objectClass=person))
        attributes:
            - { ldap_attr: uid,  user_method: setUsername }

安全.yml

encoders:
    FOS\UserBundle\Model\UserInterface: bcrypt
    AppBundle\Entity\User: plaintext

erase_credentials: false

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN]

providers:
    chain_provider:
        chain:
            providers: [fr3d_ldapbundle,fos_userbundle]

    fr3d_ldapbundle:
        id: fr3d_ldap.security.user.provider

    fos_userbundle:
        id: fos_user.user_provider.username

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        pattern:    ^/
        fr3d_ldap:  ~
        form_login:
            provider: chain_provider
            always_use_default_target_path: true
            default_target_path: /
        logout:     true
        anonymous:  true

encoders:
    AcmeBundle\Acme\User\LdapUser: plaintext

access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, role: ROLE_USER }

我使用Online LDAP Test Server测试该服务。我设法使用以下配置成功登录:

fr3d_ldap:
driver:
    host: ldap.forumsys.com
    port: 389
    #version: 3
    username: cn=read-only-admin,dc=example,dc=com
    password: password
    bindRequiresDn: true

user:
    baseDn: uid=euclid,dc=example,dc=com
    filter: (&(objectClass=person))
    attributes:
        - { ldap_attr: uid,  user_method: setUsername }

但是这个配置的问题我只能以 euclid 用户身份登录。但我想以每个可用用户的身份登录。

但是在其他配置中,我在日志中收到消息,即找到了用户,但发生了故障。

2017-04-20 09:36:16] ldap_driver.DEBUG: ldap_search(dc=example,dc=com, (&(&(objectClass=person))(uid=einstein)), [array]) {"action" :"ldap_search","base_dn":"dc=example,dc=com","filter":"(&(&(objectClass=person))(uid=einstein))","attributes":[]} [ ]

[2017-04-20 09:36:17] security.INFO:用户爱因斯坦在 LDAP {"action":"loadUserByUsername","username":"einstein","re​​sult":"found"} []

[2017-04-20 09:36:17] ldap_driver.DEBUG: ldap_bind(einstein, ****) {"action":"ldap_bind","bind_rdn":"einstein"} []

[2017-04-20 09:36:17] ldap_driver.DEBUG:异常 'Zend\Ldap\Exception\LdapException' 带有消息 '0x1:无法检索帐户的 DN:爱因斯坦 [0x1:意外结果计数(16): (&(objectClass=person))]' 在 /vendor/zendframework/zend-ldap/src/Ldap.php:805 堆栈跟踪:#0 /vendor/fr3d/ldap-bundle/Driver/ZendLdapDriver.php(82): Zend\Ldap\Ldap->bind('einstein', 'password') #1 /vendor/fr3d/ldap-bundle/Ldap/LdapManager.php(78): FR3D\LdapBundle\Driver\ZendLdapDriver->bind(Object( AppBundle\Entity\User), 'password') #2 /vendor/fr3d/ldap-bundle/Security/Authentication/LdapAuthenticationProvider.php(90): FR3D\LdapBundle\Ldap\LdapManager->bind(Object(AppBundle\Entity\用户), '密码') #3 ...

[2017-04-20 09:36:17] ldap_driver.DEBUG: ldap_search(dc=example,dc=com, (&(&(objectClass=person))(uid=einstein)), [array]) {"action ":"ldap_search","base_dn":"dc=example,dc=com","filter":"(&(&(objectClass=person))(uid=einstein))","attributes":[]} []

[2017-04-20 09:36:17] security.INFO:用户爱因斯坦在 LDAP {"action":"loadUserByUsername","username":"einstein","re​​sult":"found"} []

[2017-04-20 09:36:17] security.INFO:身份验证请求失败。{"exception":"[object] (Symfony\Component\Security\Core\Exception\BadCredentialsException(code: 0): Bad credentials. at /vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/ Provider/UserAuthenticationProvider.php:90, Symfony\Component\Security\Core\Exception\BadCredentialsException(code: 0): 提供的密码无效。在 /vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication /Provider/DaoAuthenticationProvider.php:67)"} []

我不知道我做错了什么,我刚开始使用 LDAP。

编辑 我已经更改了我的代码,就像 Alvin 发布的示例一样。但我收到以下错误:Symfony Dev Log。系统找到了用户,但随后我在日志文件中得到了错误。

4

2 回答 2

0

你只需要像这样改变你的baseDn:

user:
    baseDn: dc=example,dc=com

我认为应该这样做。


编辑#2 - 基于反馈

这是根据我的教程将其添加到 FOSUser.php 实体的解决方案:

/**
  * {@inheritDoc}
  */
public function setDn($dn){
    $this->dn = $dn;
}

/**
  * {@inheritDoc}
  */
public function getDn(){
    return $this->dn;
}

在此处参考我的教程: https ://alvinbunk.wordpress.com/2016/03/25/symfony-ad-integration/

于 2017-04-20T16:48:36.163 回答
0

所以我终于在我的代码中发现了问题。

我忘记正确设置用户实体中 DN 字段的 set 和 get 函数。那是我的代码:

/**
 * Set Ldap Distinguished Name.
 *
 * @param string $dn Distinguished Name
 */
public function setDn($dn)
{
    // TODO: Implement setDn() method.
}

/**
 * Get Ldap Distinguished Name.
 *
 * @return null|string Distinguished Name
 */
public function getDn()
{
    // TODO: Implement getDn() method.
}

但这是 Alvins 教程中正确的一个:

 /**
 * {@inheritDoc}
 */
public function setDn($dn)
{ $this->dn = $dn; }

/**
 * {@inheritDoc}
 */
public function getDn()
{ return $this->dn; }

感谢 Alvin 的帮助和精彩的教程

于 2017-05-02T08:24:31.730 回答