您好我在使用Ldap创建简单登录时遇到问题。我从spring.io网站下载了入门项目:link它与ldif文件完美配合,但我想用运行的ldap服务器替换它。我已经尝试了几天没有进展。我用这段代码得到了最好的结果(在入门项目的WebSecurityConfig中取代)
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
authManagerBuilder.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailsService());
}
@Bean
public AuthenticationManager authenticationManager() {
return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
}
@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(null, "ldap://ip:port/", "ou=GROUP,dc=domain,dc=com");
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
return provider;
}
}
如果我尝试使用“username”“password”控制台输出格式的良好用户名和密码登录:ActiveDirectoryLdapAuthenticationProvider:Active Directory身份验证失败:提供的密码无效
如果我使用“username@domain.com”和良好的密码,页面只是重新加载而没有输出到控制台。
如果我使用随机用户名和密码控制台:Active Directory身份验证失败:提供的密码无效
有人可以帮忙吗?
答案 0 :(得分:0)
正如评论中所建议的那样,我打开了日志记录,发现问题与" username@domain.com"相同。太
aciveDirectoryLdapAuthenticationProvider()出现问题,其中有3个问题。
我已从rootDn中删除了OU组并添加了域名,因此我们只能使用用户名登录。
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider("domain.com", "ldap://ip:port/", "dc=domain,dc=com");
更改了提供商的搜索过滤器
provider.setSearchFilter("(&(objectClass=user)(sAMAccountName={0}))");
最后我不得不更改ActiveDirectoryLdapAuthProvider searchForUser方法,因为它匹配" username@domain.com"使用sAMAccountName而不是"用户名"。这样:
return SpringSecurityLdapTemplate.searchForSingleEntryInternal(context,
searchControls, searchRoot, searchFilter,
new Object[] { bindPrincipal });
取而代之:
return SpringSecurityLdapTemplate.searchForSingleEntryInternal(context,
searchControls, searchRoot, searchFilter,
new Object[] { username });
完成aciveDirectoryLdapAuthenticationProvider():
@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider("domain.com", "ldap://ip:port/", "dc=domain,dc=com");
provider.setSearchFilter("(&(objectClass=user)(sAMAccountName={0}))");
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
return provider;
}
有人能为第二/第三个问题提供更好的解决方案吗?也许更好的searchfilter?我在ldap中没有匹配" username@domain.com"使用ActiveDirectoryLdapAuthProvider的bindPrincipal的格式。