如何让Spring Security的ActiveDirectoryLdapAuthenticationProvider正常工作?
Spring Boot对接Active Directory认证失败问题
我在Spring Boot应用中需要对接Active Directory完成认证,预先说明:以下ldapsearch命令可正常执行并返回预期结果:
ldapsearch -D "cn=boss,ou=admin,dc=example,dc=com" -w pw1234 -h ldap.example.com -p 389 -b "ou=students,dc=example,dc=com" "samaccountname=student42@example.com"
因此我在yaml配置中设置了LDAP上下文源如下:
spring.ldap.base: "dc=example,dc=com" spring.ldap.urls: "ldap://ldap.example.com:389" spring.ldap.username: "cn=boss" spring.ldap.password: "pw1234"
该配置在本地OpenDJ环境中使用Spring的BindAuthenticator可正常工作,但现在对接真实Active Directory时,我配置了ActiveDirectoryLdapAuthenticationProvider:
@Bean public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() { ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider("", "ldap://ldap.example.com:389"); // provider.setSearchFilter("(uid={0})"); // 是否需要此配置? return provider; }
但不幸的是,我遇到了如下错误:
ActiveDirectoryLdapAuthenticationProvider: Authentication for student42@example.com failed:javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C090439, comment: AcceptSecurityContext error, data 52e, v4563 ] ActiveDirectoryLdapAuthenticationProvider: Active Directory authentication failed: Supplied password was invalid
我无法获取更多信息,比如使用管理员DN(cn=boss)的初始绑定是否成功,但我认为还有一些配置项未设置。或许需要显式配置manager-dn和manager-password,但不知在哪里配置?有人知道该从何处着手解决此问题吗?
补充信息:
附上ldapsearch的关键结果:
dn:: Q049MmFiZWUgc3R1ZGVudCA0MiAzNDQyMDEwMTk2LE9VPXN0dWRlbnRzLERDPWV4YW1wbGUsREM9Y29t cn: 2abee student 42 3442010196 description: 3442010196 displayName: 42 student (2abee) distinguishedName:: Q049MmFiZWUgc3R1ZGVudCA0MiAzNDQyMDEwMTk2LE9VPXN0dWRlbnRzLERDPWV4YW1wbGUsREM9Y29t generationQualifier: 3442010196 givenName: 42 mail: student42@example.com memberOf:: Q049MmFiZWUsT1U9c3R1ZGVudHMsREM9ZXhhbXBsZSxEQz1jb20= name: 2abee student 42 3442010196 sn: student sAMAccountName: student42@example.com userPrincipalName: student42@example.com
登录表单中输入的凭证为:
username: student42@example.compassword: secret
内容的提问来源于stack exchange,提问作者Slevin
相关产品推荐
相关产品推荐

