1

如果用户一切正常,进行这种连接似乎可行

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: error code 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);
}

但是当我这样做时,控件为空。

有人成功地做到这一点吗?

4

1 回答 1

0

许多目录服务器使用“无效凭据”(49) 来指示帐户存在问题,即帐户不存在或身份验证存在问题。通常,最好不要指出帐户不存在或被锁定(这也表明其存在),因为这样攻击者就会知道该帐户存在。

于 2013-04-08T07:29:37.707 回答