gpt4 book ai didi

authentication - Spring 启动 ldap 安全

转载 作者:行者123 更新时间:2023-12-04 03:14:34 25 4
gpt4 key购买 nike

您好,我在使用 Ldap 创建简单登录时遇到问题。我已经从 spring.io 网站下载了入门项目:Getting started LDAP .

它与 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;
}
}

如果我尝试使用“用户名”“密码”格式的正确用户名和密码登录,控制台输出:ActiveDirectoryLdapAuthenticationProvider:Active Directory 身份验证失败:提供的密码无效

如果我使用“username@domain.com”和正确的密码,页面只会重新加载而不会向控制台输出任何内容。

如果我使用随机用户名和密码控制台:Active Directory 身份验证失败:提供的密码无效

有人可以帮忙吗?

最佳答案

按照评论中的建议,我打开了日志记录,发现问题也与“username@domain.com”相同。

问题出在 aciveDirectoryLdapAuthenticationProvider() 中,其中有 3 个问题。

  1. 我已经从 rootDn 中删除了 OU 组并添加了域,因此我们只能使用用户名登录。
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider("domain.com", "ldap://ip:port/", "dc=domain,dc=com");
  1. 更改提供商的搜索过滤器
provider.setSearchFilter("(&(objectClass=user)(sAMAccountName={0}))");
  1. 最后我不得不更改 ActiveDirectoryLdapAuthProvider searchForUser 方法,因为它匹配“username@domain.com”和 sAMAccountName 而不是“username”。这:
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;
}

有人可以为第二个/第三个问题提供更好的解决方案吗?也许更好的搜索过滤器?我在 ldap 中没有任何字段匹配使用 ActiveDirectoryLdapAuthProvider 的 bindPrincipal 的“username@domain.com”格式。

关于authentication - Spring 启动 ldap 安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42229066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com