Exemplo n.º 1
0
 public void disconnect() {
   try {
     connection.disconnect();
   } catch (LDAPException e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 2
0
  /** {@inheritDoc} */
  public LDAPConnection getConnection() throws LDAPException {

    if (M_log.isDebugEnabled()) {
      M_log.debug("getConnection()");
    }
    LDAPConnection conn = newConnection();

    if (config.isAutoBind()) {
      if (M_log.isDebugEnabled()) {
        M_log.debug("getConnection(): auto-binding");
      }
      try {
        bind(conn, config.getLdapUser(), config.getLdapPassword());
      } catch (LDAPException ldape) {
        if (ldape.getResultCode() == LDAPException.INVALID_CREDENTIALS) {
          M_log.warn(
              "Failed to bind against: "
                  + conn.getHost()
                  + " with user: "******" password: "******".", "*"));
        }
        throw ldape;
      }
    }

    return conn;
  }
Exemplo n.º 3
0
 /**
  * Determines if the supplied ldap exception should result in an operation retry.
  *
  * @param e that was produced
  * @throws LdapException wrapping the ldap exception
  */
 protected void processLDAPException(final LDAPException e) throws LdapException {
   ProviderUtils.throwOperationException(
       config.getOperationExceptionResultCodes(),
       e,
       e.getResultCode(),
       e.getMatchedDN(),
       null,
       null,
       true);
 }
Exemplo n.º 4
0
 /** {@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;
   }
 }
Exemplo n.º 5
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();
      }
    }
  }
Exemplo n.º 6
0
 /**
  * Determines whether the supplied ldap exception should be ignored.
  *
  * @param ignoreResultCodes to match against the exception
  * @param e ldap exception to match
  * @return result code that should be ignored or null
  */
 protected ResultCode ignoreSearchException(
     final ResultCode[] ignoreResultCodes, final LDAPException e) {
   ResultCode ignore = null;
   if (ignoreResultCodes != null && ignoreResultCodes.length > 0) {
     for (ResultCode rc : ignoreResultCodes) {
       if (e.getResultCode() == rc.value()) {
         logger.debug("Ignoring ldap exception", e);
         ignore = rc;
         break;
       }
     }
   }
   return ignore;
 }
Exemplo n.º 7
0
    /** {@inheritDoc} */
    @Override
    public boolean hasNext() throws LdapException {
      if (resultIterator == null || response != null) {
        return false;
      }

      boolean more = false;
      try {
        more = resultIterator.hasNext();
        if (!more) {
          final LDAPResponse res = resultIterator.getResponse();
          logger.trace("reading search response: {}", res);
          throwOperationException(request, res);
          response = createResponse(request, null, res);
        }
      } catch (LDAPException e) {
        final ResultCode rc = ignoreSearchException(config.getSearchIgnoreResultCodes(), e);
        if (rc == null) {
          processLDAPException(e);
        }
        response = new Response<Void>(null, rc, e.getLDAPErrorMessage(), null, null, null, -1);
      }
      return more;
    }
Exemplo n.º 8
0
 public LDAPUser login(String aUserName, String aPassword) throws InvalidLoginException {
   LDAPUser onlineUser = null;
   try {
     connection.connect(ldapHost, ldapPort);
     System.out.println("Connect Successfull");
     System.out.println(aUserName);
     LDAPSearchQueue searchResults =
         connection.search(
             "o=Aerothai",
             LDAPConnection.SCOPE_SUB,
             "cn=" + aUserName,
             new String[] {LDAPConnection.NO_ATTRS},
             true,
             (LDAPSearchQueue) null);
     LDAPMessage message;
     message = searchResults.getResponse();
     if (message instanceof LDAPSearchResult) {
       LDAPEntry entry = ((LDAPSearchResult) message).getEntry();
       String dn = entry.getDN();
       String[] userDn = dn.split(",");
       String fullDn = userDn[0] + "," + userDn[1] + "," + userDn[2] + ",o=Aerothai";
       connection.bind(ldapVersion, fullDn, aPassword.getBytes("UTF8"));
       System.out.println("Bind Successfull");
       onlineUser = new LDAPUser();
       try {
         onlineUser.setFirstName((String) getAttribute(fullDn, "givenName").elementAt(0));
         onlineUser.setLastName((String) getAttribute(fullDn, "sn").elementAt(0));
         try {
           Integer.parseInt((String) getAttribute(fullDn, "cn").elementAt(1));
           onlineUser.setEmployeeCode((String) getAttribute(fullDn, "cn").elementAt(1));
         } catch (NumberFormatException e) {
           onlineUser.setEmployeeCode((String) getAttribute(fullDn, "cn").elementAt(0));
         }
         onlineUser.setDepartment((String) getAttribute(fullDn, "ou").elementAt(0));
         onlineUser.setLocation((userDn[2].split("="))[1]);
       } catch (ArrayIndexOutOfBoundsException e1) {
         e1.printStackTrace();
         onlineUser = null;
         throw new InvalidLoginException(
             "à¡Ô´¤ÇÒÁ¼Ô´¾ÅÒ´ÃÐËÇèÒ§¡Òô֧¢éÍÁÙÅ ¡ÃØ³Ò Login ãËÁèÍÕ¡¤ÃÑé§");
       }
     } else {
       disconnect();
       throw new InvalidLoginException(
           "äÁ辺¼Ùéãªé§Ò¹ª×èÍ "
               + aUserName
               + " ¡ÃسÒÅͧÍÕ¡¤ÃÑé§ ËÃ×Í µÔ´µèÍà¨éÒ˹éÒ·Õè¡Í§Ç¤.¾Ç. à¾×è͵ÃǨÊͺ¢éÍÁÙŢͧ·èÒ¹");
     }
     disconnect();
   } catch (LDAPException e) {
     e.printStackTrace();
     throw new InvalidLoginException(
         "ÃËÑʼèÒ¹äÁè¶Ù¡µéͧ ¡ÃسÒÅͧÍÕ¡¤ÃÑé§ ËÃ×Í µÔ´µèÍà¨éÒ˹éÒ·Õè¡Í§Ç¤.¾Ç. à¾×è͵ÃǨÊͺ¢éÍÁÙŢͧ·èÒ¹");
   } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
     throw new InvalidLoginException(e);
   } finally {
     disconnect();
   }
   return onlineUser;
 }