问题描述
我正在使用 Symfony 5.1 并尝试实现 LDAP 身份验证,而用户属性(用户名、角色等)存储在 MySQL 数据库中.因此,我为 Doctrine 添加了一个用户实体,并配置了文档对应的 services.yml 和 security.yml.
I'm using Symfony 5.1 and trying to implement a LDAP Authentication, while the User Properties (Username, Roles, etc.) are stored in a MySQL DB. Thus I added a User Entity for Doctrine and configurated the services.yml and security.yml corresponding to the Documentation.
我还使用 Maker Bundle 生成了一个 LoginFormAuthenticator
,它似乎使用了 Guard Authenticator 模块.
I also used the Maker Bundle to generate a LoginFormAuthenticator
which seems to use the Guard Authenticator Module.
当我尝试登录时,它看起来好像没有做任何与 LDAP 相关的事情.我还用 tcpdump 监听了 TCP 包,没有看到任何到 LDAP 服务器的流量.
When I'm trying to login it simply looks like it is not doing anything LDAP related. I also listened the TCP packages with tcpdump and didn't see any traffic to the LDAP server.
这是我的代码:
services.yml
:
security.yml
:
LoginFormAuthenticator
,我猜问题出在 checkCredentials
函数中.我发现 LdapBindAuthenticationProvider
类的目的似乎正是针对 LDAP 进行此类用户凭据检查,但我完全不确定该怎么做:
The LoginFormAuthenticator
, I guess the issue lies here within the checkCredentials
function. I found the LdapBindAuthenticationProvider
class which's purpose seems to be exactly such user credential checking agains LDAP, but I'm totally unsure how I have to do it:
很遗憾,我没有找到任何示例代码.
Unfortunately I didn't find any example code for this.
感谢 T. van den Berg 的回答,我终于设法让身份验证部分正常工作.我从 security.yml 中删除了 LoginFormAuthenticator Guard,并稍微调整了 form_login_ldap.
Thanks to the answer of T. van den Berg I finally managed to get the authentication part working. I removed the LoginFormAuthenticator Guard from the security.yml and tweaked the form_login_ldap a little bit.
它现在使用 LDAPUserProvider 使用 LDAP 服务用户(绑定 DN)通过其登录名(sAMAccountName)获取用户 LDAP 对象,然后在第二个请求中使用此 LDAP 对象的专有名称(DN)来使用提供的密码对 LDAP 服务器进行另一次身份验证.到目前为止还不错.
It is now using the LDAPUserProvider to use the LDAP service user (bind DN) to fetch the user LDAP object by its login name (sAMAccountName) and then in a second request use the distinguished name (DN) of this LDAP object to make another authentication against the LDAP server with the provided password. That's fine so far.
唯一缺少的是数据库存储的用户实体.我的想法如下:
The only thing missing is the database stored User entity. My Idea was as follows:
- 登录表单已提交
- 使用提供的用户名在数据库中搜索用户实体
- 如果未找到用户实体,请使用 LDAPUserProvider 向 LDAP 询问用户名
- 如果用户存在于 LDAP 中,则在数据库中创建一个用户实体
- 使用提供的密码针对 LDAP 对用户进行身份验证
密码未保存在数据库中,但其他应用程序特定信息在 LDAP 中不可用(例如上次活动).
The password is not saved in the database, but other application specific information not available in LDAP (e.g. last activity).
有人知道如何实现吗?
推荐答案
如果您想将 LDAP 用户保存到登录后的本地数据库.
You can use this bundle ldaptools/ldaptools-bundle (or Maks3w/FR3DLdapBundle)
if you want to save your LDAP user to a local database after they login.
有关更多信息,请参阅:https://github.com/ldaptools/ldaptools-bundle/blob/master/Resources/doc/Save-LDAP-Users-to-the-Database-After-Login.md
for more information see: https://github.com/ldaptools/ldaptools-bundle/blob/master/Resources/doc/Save-LDAP-Users-to-the-Database-After-Login.md
这就是我的工作方式(没有外部捆绑包):
- Security.yaml
- 事件监听器
- services.yaml
这篇关于Symfony 5.1:使用实体用户提供者的 LDAP 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!