做這樣的連接,似乎工作,如果一切都確定了用戶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