2017-04-20 140 views
1

我正在使用帶有FOSUserBundle的FR3DLdapBundle。FR3DLdapBundle - 身份驗證請求失敗

Symfony的版本3.2.6 FR3DLdapBundle 3版

config.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 } 

security.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 } 

但有了這個配置我只能登錄爲用戶歐幾里得的問題。但我想以每個可用用戶的身份登錄。

但與其他配置我得到的消息在日誌中,該用戶被發現,但一個期待已發生。

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: User einstein found on LDAP {"action":"loadUserByUsername","username":"einstein","result":"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: exception 'Zend\Ldap\Exception\LdapException' with message '0x1: Failed to retrieve DN for account: einstein [0x1: Unexpected result count (16) for: (&(objectClass=person))]' in /vendor/zendframework/zend-ldap/src/Ldap.php:805 Stack trace: #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\User), 'password') #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: User einstein found on LDAP {"action":"loadUserByUsername","username":"einstein","result":"found"} []

[2017-04-20 09:36:17] security.INFO: Authentication request failed. {"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): The presented password is invalid. at /vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php:67)"} []

我不知道我做錯了什麼,我在我的開始與LDAP。

編輯 我改變了我的代碼,就像Alvin發佈的例子。 但我收到以下錯誤:Symfony Dev Log。 系統找到用戶,但然後我得到日誌文件中的錯誤。

+0

您是否明確要使用FR3D LDAP Bundle,或者您的目標是使用任何可用的可能性/工具來針對LDAP服務器驗證用戶身份?如果答案是第二個選項,那麼您應該知道Symfony從版本2開始支持LDAP。8並且不需要外部捆綁。 – cezar

+0

我只想對LDAP服務器進行身份驗證。我發現了這個包,並認爲它會很有用。 – tablesuplex

+0

此外,仔細檢查你根據我的文章所做的一切。我認爲當我第一次寫文章時,我必須從頭開始創建一些Symfony項目,直到我開始工作。對於我現在的新項目,我只是複製所有相同的設置。不要忘記更新架構。 –

回答

0

所以我finaly發現我的代碼中的問題。

我忘了爲用戶實體中的DN字段正確設置和獲取函數。這是我的代碼:

/** 
* 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; } 

謝謝阿爾文的幫助和真棒tutorial

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/

+0

嗨,那是我之前使用的baseDn。但有了這個配置,我只能以用戶euclid的身份登錄,而不能像其他任何用戶那樣登錄。這是我的問題。 – tablesuplex

+1

嗨複式。我看到你的配置與我的配置有點不同。驗證到測試LDAP服務器確實工作,我知道我以前做過。我目前也將它用於Active Directory。看看我的博客,瞭解我的配置和你的配置之間的區別:https://alvinbunk.wordpress.com/2016/03/25/symfony-ad-integration/ –

+0

謝謝Alvin的偉大教程,但我已經改變了所有像你一樣的文件,但我得到以下錯誤:[開發日誌](https://www.dropbox.com/s/ctgftjyp7fjepkv/dev.log?dl=0) 唯一不同於你的文件是用戶實體的名稱。 – tablesuplex