Java 不使用admin/manager帐户的Spring LDAP身份验证
我的要求是验证用户的登录名(用户名和密码),以便能够访问web应用程序 使用Java,我能够使用用户提供的登录进行身份验证:Java 不使用admin/manager帐户的Spring LDAP身份验证,java,spring,active-directory,ldap,Java,Spring,Active Directory,Ldap,我的要求是验证用户的登录名(用户名和密码),以便能够访问web应用程序 使用Java,我能够使用用户提供的登录进行身份验证: boolean isCredentialsValid = false; Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ld
boolean isCredentialsValid = false;
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
try {
DirContext ctx = new InitialDirContext(env);
ctx.close();
isCredentialsValid = true;
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(isCredentialsValid);
这能在春季直接实现吗?比如使用BindAuthenticator或PasswordComparisonAuthenticator?我尝试使用此配置:
<bean id="contextSource"
class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg value="IP of LDAP Server:389" />
</bean>
<bean id="ldapAuthenticator"
class="org.springframework.security.providers.ldap.authenticator.BindAuthenticator">
<constructor-arg ref="contextSource"/>
<property name="userDNPatterns" >
<list>
<value>uid={0}, dc=company,dc=com</value>
</list>
</property>
</bean>
<bean id="userSearch"
class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
<constructor-arg index="0" value="ou=people" />
<constructor-arg index="1" value="(uid={0})" />
<constructor-arg index="2" ref="contextSource" />
<property name="searchSubtree" value="true" />
</bean>
uid={0},dc=company,dc=com
但我遇到了未分类的例外
从目前为止我在SpringLDAP上读到的内容来看,如果不提供管理员帐户,我无法直接验证用户的登录
非常感谢您的帮助强>
这是stacktrace:
LDAP处理过程中发生未分类异常;嵌套异常为javax.naming.NamingException:[LDAP:错误代码1-000004DC:LDAPPER:DSID-0C0906DC,注释:为了执行此操作,必须在连接上完成成功绑定,数据0,v1db0];剩余名称“uid”=username@company.com'; 嵌套异常为org.springframework.ldap.UncategorizedLdapException:ldap处理过程中发生未分类异常;嵌套异常为javax.naming.NamingException:[LDAP:错误代码1-000004DC:LDAPPER:DSID-0C0906DC,注释:为了执行此操作,必须在连接上完成成功绑定,数据0,v1db0];剩余名称“uid”=username@company.com"
如果这是针对windows active directory的,请尝试spring的ActiveDirectoryLdapAuthenticationProvider。您是否尝试过用于ldap的JAAS模块(com.sun.security.auth.module.LdapLoginModule)?LdapTemplate(spring ldap core 1.3.1RELEASE)在这种情况下是无用的(糟糕的是,我相信它对其他人也是无用的)-因为在LdapTemplate.authenticate(…)方法上执行搜索,如果不先成功绑定,显然无法完成搜索 我采用了与ActiveDirectoryLdapAuthenticationProvider相同的概念:
private static DirContext bindAsUser(String username, String password) {
final String bindUrl = url;
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.SECURITY_AUTHENTICATION, "simple");
String bindPrincipal = username;
env.put(Context.SECURITY_PRINCIPAL, bindPrincipal);
env.put(Context.PROVIDER_URL, bindUrl);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
try {
return contextFactory.createContext(env);
} catch (NamingException e) {
throw new RuntimeException(e);
}
}
private static DirContext bindauser(字符串用户名、字符串密码){
最后一个字符串bindUrl=url;
Hashtable env=新的Hashtable();
环境put(Context.SECURITY_认证,“simple”);
字符串bindPrincipal=用户名;
环境保护(Context.SECURITY_PRINCIPAL,bindPrincipal);
env.put(Context.PROVIDER\uURL,bindUrl);
环境放置(Context.SECURITY\u凭证、密码);
put(Context.INITIAL\u Context\u工厂,“com.sun.jndi.ldap.LdapCtxFactory”);
试一试{
返回contextFactory.createContext(env);
}捕获(NamingE例外){
抛出新的运行时异常(e);
}
}
SpringLDAP核心1.3.1版本的另一个问题是,我无法理解为什么它没有合并到SpringSecurityLDAP:3.1。@nico_ekito我刚刚添加了stacktrace。谢谢,这是针对Windows Active Directory的。我会检查一下,如果我能让它工作,我会试试的。非常感谢。@drobson请在您的答案中包含更多内容,而不仅仅是链接,这可能会在将来失效。