Esempio n. 1
0
 public void disconnect() {
   try {
     connection.disconnect();
   } catch (LDAPException e) {
     e.printStackTrace();
   }
 }
 /** {@inheritDoc} */
 public void returnConnection(LDAPConnection conn) {
   try {
     if (conn != null) conn.disconnect();
   } catch (LDAPException e) {
     M_log.error("returnConnection(): failed on disconnect: ", e);
   }
 }
  /**
   * Connects the specified <code>LDAPConnection</code> to the currently configured host and port.
   *
   * @param conn an <code>LDAPConnection</code>
   * @throws LDAPConnection if the connect attempt fails
   */
  protected void connect(LDAPConnection conn) throws LDAPException {
    if (M_log.isDebugEnabled()) {
      M_log.debug("connect()");
    }

    conn.connect(config.getLdapHost(), config.getLdapPort());

    try {
      postConnect(conn);
    } catch (LDAPException e) {
      M_log.error(
          "Failed to completely initialize a connection [host = "
              + config.getLdapHost()
              + "][port = "
              + config.getLdapPort()
              + "]",
          e);
      try {
        conn.disconnect();
      } catch (LDAPException ee) {
      }

      throw e;
    } catch (Throwable e) {
      M_log.error(
          "Failed to completely initialize a connection [host = "
              + config.getLdapHost()
              + "][port = "
              + config.getLdapPort()
              + "]",
          e);
      try {
        conn.disconnect();
      } catch (LDAPException ee) {
      }

      if (e instanceof Error) {
        throw (Error) e;
      }
      if (e instanceof RuntimeException) {
        throw (RuntimeException) e;
      }

      throw new RuntimeException("LDAPConnection allocation failure", e);
    }
  }
 /** {@inheritDoc} */
 @Override
 public void close(final RequestControl[] controls) throws LdapException {
   try {
     if (connection != null) {
       connection.disconnect(getLDAPConstraints(controls));
     }
   } catch (LDAPException e) {
     throw new LdapException(e, ResultCode.valueOf(e.getResultCode()));
   } finally {
     connection = null;
   }
 }
Esempio n. 5
0
  public boolean checkLdapLoginCredential(String userName, String userPassword) throws Exception {
    LDAPConnection lc = new LDAPConnection();
    int ldapVersion = LDAPConnection.LDAP_V3;
    boolean ritorno = false;

    try {
      // check for required data
      if (isNullValue(ApplicationConfigurator.HOST)
          || (isNullValue(ApplicationConfigurator.PORT)
              && isNullValue(ApplicationConfigurator.PORT_SSL))
      /*
      || isNullValue(ApplicationConfigurator.SEARCH_PATH)
      || isNullValue(ApplicationConfigurator.USER_DN_ATTRIBUTE_NAME)
       */ ) {
        throw new Exception("Dati obbligatori per la connessione ad LDAP non presenti.");
      }

      // Connect to the server
      try {
        lc.connect(ApplicationConfigurator.HOST, Integer.parseInt(ApplicationConfigurator.PORT));
      } catch (Exception e) {
        e.printStackTrace();
        throw e;
      }

      // Try login with user credential
      try {
        if (isNullValue(userName) || isNullValue(userPassword)) return false;

        userName =
            isNullValue(ApplicationConfigurator.USER_DN_ATTRIBUTE_NAME)
                ? userName
                : ApplicationConfigurator.USER_DN_ATTRIBUTE_NAME + "=" + userName;
        userName +=
            isNullValue(ApplicationConfigurator.SEARCH_PATH)
                ? ""
                : "," + ApplicationConfigurator.SEARCH_PATH;

        lc.bind(ldapVersion, userName, userPassword);
      } catch (Exception e) {
        // Se si verifica un eccezione in questo punto significa che l'utente non è stato
        // trovato, per questo motivo risollevo l'eccezione con il messaggio standard di
        // utente e/o password errati.
        e.printStackTrace();
        throw new Exception(ApplicationConfigurator.LanguageManager.getString("LDAP.Msg4"));
      }
      ritorno = true;
    } finally {
      // disconnect with the server
      lc.disconnect();
      return ritorno;
    }
  }
Esempio n. 6
0
  public static void main(String[] args) {
    String ldapHost = "192.168.121.130";
    String loginDN = "cn=admin,dc=ucweb,dc=com";
    String password = "******";
    String containerName = "dc=ucweb,dc=com";

    int ldapPort = LDAPConnection.DEFAULT_SSL_PORT;
    int ldapVersion = LDAPConnection.LDAP_V3;

    LDAPJSSESecureSocketFactory ssf =
        new LDAPJSSESecureSocketFactory(TrustManager.createSSLSocketFactory());
    LDAPConnection lc = new LDAPConnection(ssf);

    LDAPAttributeSet attributeSet = new LDAPAttributeSet();

    attributeSet.add(
        new LDAPAttribute("objectclass", new String[] {new String("top"), new String("person")}));
    attributeSet.add(new LDAPAttribute("cn", "17"));
    attributeSet.add(new LDAPAttribute("sn", "17"));
    attributeSet.add(new LDAPAttribute("description", " "));
    //        attributeSet.add(new LDAPAttribute("userPassword", "111111"));
    String dn = "cn=17," + containerName;
    LDAPEntry newEntry = new LDAPEntry(dn, attributeSet);

    try {
      lc.connect(ldapHost, ldapPort);
      lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));
      System.out.println("login ldap server successfully.");
      lc.add(newEntry);
      System.out.println("Added object: " + dn + " successfully.");

    } catch (LDAPException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } finally {
      try {
        if (lc.isConnected()) {
          lc.disconnect();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }