我创建了一个本地LDAP服务器,并添加了密码为“123456”的用户“djiao”
尝试使用Spring Boot实现Spring Security的身份验证。我的webconfig类如下:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin();
}
@Bean
public ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider("", "ldap://localhost:10389");
provider.setConvertSubErrorCodesToExceptions(true);
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
return provider;
}
@Bean
public LoggerListener loggerListener() {
return new LoggerListener();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
}
但是我似乎无法从登录页面登录。
如果我使用djiao(cn)或djiao1(uid),我会得到500。
[LDAP:错误代码34 - 给出错误的DN:djiao1(0x64 0x6A 0x69 0x61 0x6F 0x31)无效];嵌套异常是javax.naming.InvalidNameException:[LDAP:错误代码34 - 给出不正确的DN:djiao1(0x64 0x6A 0x69 0x61 0x6F 0x31)无效]
如果我使用dn“cn = djiao,ou = Users,dc = example,dc = com”作为用户名,我将收到“错误凭据”错误。密码只是123456.
登录用户名应该是什么?或者我在websecurityconfig类中遗漏了什么?
答案 0 :(得分:5)
因为从您的代码中我可以确定您正在使用Spring-Boot。
这是我们连接到LDAP
的工作原理@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder authBuilder) throws Exception {
authBuilder
.ldapAuthentication()
.userSearchFilter("(sAMAccountName={0})")
.userSearchBase("dc=some,dc=domain,dc=com")
.groupSearchBase("ou=groups,dc=some,dc=domain,dc=com")
.groupSearchFilter("member={0}")
.contextSource()
.url("ldaps://<ldap-server>")
.port(639)
.managerDn("cn=binduser,ou=users,dc=some,dc=domain,dc=com")
.managerPassword("some pass")
;
}
}
所以从本质上来说,userSearchFilter
你必须定义不同的值。如果您使用除AD以外的任何LDAP,则您的过滤器应为"(uid={0})"
,或者如果您不想让人们使用该电子邮件,您也可以使用"(mail={0})"
或"(|(uid={0})(mail={0}))"
之类的组合哪个woul允许使用它们。
如果您选择ActiveDirectory - 我认为您不是基于上面所写的内容 - 它应该是sAMAccountName
,如上所述,允许人们在域中输入他们的ID,如{{1所以登录只是MYDOMAIN\myusername
。
如果您需要连接到多个LDAP服务器,这些LDAP服务器为HA目的共享相同的信息,您可以通过myusername
调用来执行此操作。如果它们带有不同的,例如&#39; EMEA&#39;,&#39; US&#39;&#39; AP&#39;您可以使用以下方法组合这些电话:
.contextSource().url()
BTW:这也允许您将不同的身份验证机制(如InMemory(Default-Admin-Backdoor))与LDAP和/或JDBC结合使用。