Spring boot ActiveDirectoryLdapAuthenticationProvider |如何提供主要凭据
我正在开发SpringBoot应用程序。我对如何在AuthenticationManagerBuilder的身份验证提供程序ActiveDirectoryLdapAuthenticationProvider对象中设置主体凭据感到困惑。它只有setSearchFilter来查找用户是否在组中。但是,如何为验证web用户提供主要凭据?我注意到,当我提供正确的凭据时,重新加载需要2秒钟,但身份验证仍然失败:Spring boot ActiveDirectoryLdapAuthenticationProvider |如何提供主要凭据,spring-boot,spring-security,ldap,spring-ldap,Spring Boot,Spring Security,Ldap,Spring Ldap,我正在开发SpringBoot应用程序。我对如何在AuthenticationManagerBuilder的身份验证提供程序ActiveDirectoryLdapAuthenticationProvider对象中设置主体凭据感到困惑。它只有setSearchFilter来查找用户是否在组中。但是,如何为验证web用户提供主要凭据?我注意到,当我提供正确的凭据时,重新加载需要2秒钟,但身份验证仍然失败:(登录?错误重定向)。但对于错误的Credentail,将抛出无效密码消息 另外,如何将用户名从
(登录?错误重定向)
。但对于错误的Credentail,将抛出无效密码消息
(&(objectClass=user)(userPrincipalName={0})(memberOf=groupname))
这是失败的,userPrincipalName={0}在这里代表什么
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception
{
ActiveDirectoryLdapAuthenticationProvider adProvider =
new ActiveDirectoryLdapAuthenticationProvider(domain, url, userBaseDn);
adProvider.setConvertSubErrorCodesToExceptions(true);
adProvider.setUseAuthenticationRequestCredentials(true);
// set pattern if it exists
// The following example would authenticate a user if they were a member
// of the ServiceAccounts group
// (&(objectClass=user)(userPrincipalName={0})
// (memberof=CN=ServiceAccounts,OU=alfresco,DC=mycompany,DC=com))
if (userDnPattern != null && userDnPattern.trim().length() > 0)
{
adProvider.setSearchFilter(userDnPattern);
}
auth.authenticationProvider(adProvider);
// don't erase credentials if you plan to get them later
// (e.g using them for another web service call)
auth.eraseCredentials(false);
}
我可以回答你的一些问题,但分享你的主要问题 2和3的答案是相关的
(&(objectClass=user)(userPrincipalName={0})
是默认的搜索筛选器,可以替换为
sAMAccountName={0}
对于过滤器,如果您发现它适用于您的设置
((&cn={0})) // works for me
这只是与通过LDAP返回的AD记录中的字段匹配,表达式{0}
是搜索时替换凭证id的地方。您需要调整此语句以匹配目录结构和要应用于Active directory搜索的筛选。提供太少或不够具体的筛选条件将引发异常,如:
应为1,但找到5
然而,和您一样,我怀疑但不知道是否可以或应该直接设置主要安全性。通过查看,它似乎是一个使用Spring的自动魔法函数,因为这样做的方法被隐藏在隐藏的ContextFactory中
但是,如果您愿意为此尝试其他Spring类,请突出显示将GlobalAuthenticationConfigurerAdapter作为嵌套类扩展到WebSecurityConfigureAdapter,并使用该类设置基本DN:
contextSource.setBase("OU=MyCo Global,DC=myco");
完整代码取自上面链接的示例:
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/css/**").permitAll().anyRequest()
.fullyAuthenticated().and().formLogin();
}
@Configuration
protected static class AuthenticationConfiguration extends
GlobalAuthenticationConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource("ldap://<url>");
contextSource.setUserDn("<username>");
contextSource.setPassword("<password>");
contextSource.setReferral("follow");
contextSource.afterPropertiesSet();
LdapAuthenticationProviderConfigurer<AuthenticationManagerBuilder> ldapAuthenticationProviderConfigurer = auth.ldapAuthentication();
ldapAuthenticationProviderConfigurer
.userSearchFilter("(&(cn={0}))")
.userSearchBase("")
.contextSource(contextSource);
}
}
}
@配置
@启用WebMVC安全性
公共类SecurityConfig扩展了WebSecurity配置适配器{
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http.authorizeRequests().antMatchers(“/css/**”).permitAll().anyRequest()
.fullyAuthenticated()和().formLogin();
}
@配置
受保护的静态类AuthenticationConfiguration扩展
GlobalAuthenticationConfigurerAdapter{
@凌驾
public void init(AuthenticationManagerBuilder auth)引发异常{
DefaultSpringSecurityContextSource contextSource=新的DefaultSpringSecurityContextSource(“ldap://”);
contextSource.setUserDn(“”);
contextSource.setPassword(“”);
contextSource.setReferral(“follow”);
contextSource.AfterPropertieSet();
LdapAuthenticationProviderConfigurer LdapAuthenticationProviderConfigurer=auth.ldapAuthentication();
ldapAuthenticationProviderConfigurer
.userSearchFilter((&(cn={0})))
.userSearchBase(“”)
.contextSource(contextSource);
}
}
}