使用Spring Security ActiveDirectoryLdapAuthenticationProvider问题在Active Directory上进行LDAP身份验证

时间:2017-10-12 17:58:08

标签: filter active-directory ldap username

基于My Spring Security 4.2.3的项目必须使用LDAP v3针对MS Active Directory对用户进行身份验证。 探索LDAP服务器树我发现必须使用以下LDAP过滤器来搜索单个用户条目:

(及(objectClass的=用户)(sAMAccountName赋=))

请注意,您必须在过滤器中提供"用户名" ,而不是" username @ domain" - 很遗憾,有& #39;包含 username @ domain

的LDAP模式中的任何目录条目属性

我将ActiveDirectoryLdapAuthentication类配置为身份验证提供程序,并传递域" mydomain.com "在类构造函数中。

用户搜索过滤器是  " (及(objectClass的=用户)(sAMAccountName赋= {0}))"

在对doAuthentication()方法的调用中,身份验证过程始终失败(凭据100%检查)。

挖掘源代码我发现LDAP绑定操作正常(对 bindAsUser(用户名,密码)的调用没问题)但是调用 searchForUser(ctx,username) 总是因以下原因而失败:

SpringSecurityLdapTemplate.searchForSingleEntryInternal(...)的调用会收到" username@mydomain.com"作为要在过滤器的{0}表达式中替换的用户名和字符串" username@mydomain.com"永远不会匹配价值"用户名"包含在用户条目的 sAMAccountName 属性中。

我试图解决子类化ActiveDirectoryLdapAuthentication但不幸的是它被声明为final(为什么?)

唯一的解决方案(我并不为此感到自豪,相信我)我发现将整个ActiveDirectoryLdapAuthentication代码剪切并粘贴到新的MyActiveDirectoryLdapAuthentication类中,将 username 作为{0}参数传递而不是 username@mydomain.com SpringSecurityLdapTemplate.searchForSingleEntryInternal(...)调用。

它有效,但这是一个肮脏的把戏。如何以更优雅和正确的方式解决这个问题?

0 个答案:

没有答案