我将FR3DLdapBundle与FOSUserBundle结合使用。
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 }我用联机LDAP测试服务器测试服务。我成功地使用以下配置成功登录:
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:16] "filter":"(&(&(objectClass=person))(uid=einstein))",:ldap_driver.DEBUG: ldap_search(dc=example,dc=com,(&(&(objectClass=person))(uid=einstein)),数组) {"action":"ldap_search","base_dn":"dc=example,dc=com",[]} security.INFO:用户爱因斯坦在LDAP {"action":"loadUserByUsername",“username”:“爱因斯坦”,ldap_driver.DEBUG: ldap_bind(爱因斯坦,*){“动作”:“ldap_bind”,“/vendor/zendframework/zend-ldap/src/Ldap.php:805”:“bind_rdn”} ldap_driver.DEBUG:异常'Zend\Ldap\ exception \LdapException‘,消息'0x1:未能检索帐户DN :bind_rdn 0x1:意外结果计数(16)用于:(&(objectClass=person))在/vendor/fr3d/ldap-bundle/Driver/ZendLdapDriver.php(82):堆栈跟踪中:#0 /vendor/fr3d/ldap-bundle/Driver/ZendLdapDriver.php(82):Zend\Ldap\Ldap->绑定(’/vendor/fr3d/ldap-bundle/Driver/ZendLdapDriver.php(82):‘,(密码) #1 /vendor/fr3d/ldap-bundle/Ldap/LdapManager.php(78):FR3D\LdapBundle\Driver\ZendLdapDriver->bind(Object(AppBundle\Entity\User),‘密码’) #2 /vendor/fr3d/ldap-bundle/Security/Authentication/LdapAuthenticationProvider.php(90):FR3D\LdapBundle\Ldap\LdapManager->bind(Object(AppBundle\Entity\User),‘密码’) #3 . 2017-04-20 09:17 ldap_driver.DEBUG: ldap_search(dc=example,dc=com,(&(&(objectClass=person))(uid=einstein)),数组) {"action":"ldap_search","base_dn":"dc=example,dc=com",“base_dn”:[]} security.INFO:用户爱因斯坦在LDAP {"action":"loadUserByUsername",“username”:“爱因斯坦”,“结果”:“找到”} security.INFO:身份验证请求失败。{“例外”:“对象”} []
我不知道自己做错了什么,我刚开始使用LDAP。
编辑我已经修改了我的代码,就像Alvin发布的示例一样。但是我得到了以下错误:Symfony发展日志。系统创建用户,但我得到日志文件中的错误。
发布于 2017-05-02 08:24:31
所以我终于在我的代码中找到了问题。
我忘记为用户实体中的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; }感谢阿尔文的帮助和了不起的教程。
发布于 2017-04-20 16:48:36
您只需要像这样更改您的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/
https://stackoverflow.com/questions/43513525
复制相似问题