有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 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。命名。AuthenticationException:LDAP:错误代码49 这并不能区分无效通行证和过期账户等

我了解到您可以尝试重新连接,然后执行GetResponseControl()以访问实际发生的详细错误,例如:

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);
}

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

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


共 (1) 个答案

  1. # 1 楼答案

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