做这样的连接,似乎工作,如果一切都确定了用户LDAP连接错误,让细节子错误代码
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
props.setProperty(Context.PROVIDER_URL, someUrl);
props.setProperty(Context.SECURITY_PRINCIPAL, commonName);
props.setProperty(Context.SECURITY_CREDENTIALS, password);
InitialLdapContext ctx = null;
try {
ctx = new InitialLdapContext(props, null);
return true;
} catch (javax.naming.AuthenticationException ex) {
ex.printStackTrace();
} catch (NamingException ex) {
logger.warn("LDAP NamingException looking for user - " +
username + ": " + ex.getMessage(), ex);
}
return false;
但是,如果用户是无效的,我们只是得到这样一个错误: 的javax .naming.AuthenticationException:LDAP:错误代码49 它不会告诉无效通过与过期帐户之间的差异等
我读过,你可以尝试做一个重新连接,然后做getResponseControls()获得访问实际发生的详细错误,如:
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
props.setProperty(Context.PROVIDER_URL, someUrl);
InitialLdapContext ctx = null;
try {
ctx = new InitialLdapContext(props, null);
// set the user/pass they entered
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, commonName);
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
Hashtable env = new Hashtable();
ctx.reconnect(null);
return true;
} catch (javax.naming.AuthenticationException ex) {
ex.printStackTrace();
try {
Control[] controls = ctx.getResponseControls();
if (controls == null) {
logger.debug("response controls = null", ex);
} else {
logger.debug("response controls = " + controls.toString(), ex);
}
} catch (NamingException ex1) {
logger.error("error decoding respponsecontrols", ex);
}
return false;
} catch (NamingException ex) {
logger.warn("LDAP NamingException looking for user - " +
username + ": " + ex.getMessage(), ex);
}
但是当我这样做,控件是空的。
有人曾经成功做过这件事吗?
的地方我看了有关使用重新连接()来获取访问CTX对象是:http://bb10.com/apache-directory-user/2011-11/msg00057.html但正如我所说,这确实似乎没有工作,因为ctx.getResponseControls()为空。期待任何人有任何想法。 – 2013-04-08 23:02:06
你有没有找到解决这个问题的办法? – kukudas 2014-03-06 17:37:10