Java Code Examples for javax.naming.ldap.InitialLdapContext#close()

The following examples show how to use javax.naming.ldap.InitialLdapContext#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LdapUsersLoginModule.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bind to the LDAP server for authentication
 */
private boolean createLdapInitContext(String username, Object credential) throws Exception
{
   // Get the admin context for searching
   InitialLdapContext ctx = null;
   ClassLoader currentTCCL = SecurityActions.getContextClassLoader();
   try
   {
      if (currentTCCL != null)
         SecurityActions.setContextClassLoader(null);
      ctx = constructInitialLdapContext(bindDN, bindCredential);
      // Validate the user by binding against the userDN
      bindDNAuthentication(ctx, username, credential, baseDN, baseFilter);
   }
   catch(Exception e)
   {
 	  throw e;
   }
finally
   {
      if (ctx != null)
         ctx.close();
      if (currentTCCL != null)
         SecurityActions.setContextClassLoader(currentTCCL);
   }
   return true;
}
 
Example 2
Source File: LdapCallbackHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void safeClose(InitialLdapContext ic)
{
	if(ic != null)
	{
		try
		{
			ic.close();
		}
		catch (NamingException e)
		{
		}
	}
}
 
Example 3
Source File: LdapUsersLoginModule.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected String bindDNAuthentication(InitialLdapContext ctx, String user, Object credential, String baseDN,
      String filter) throws NamingException
{
   SearchControls constraints = new SearchControls();
   constraints.setSearchScope(searchScope);
   constraints.setTimeLimit(searchTimeLimit);
   String attrList[] = {distinguishedNameAttribute};
   constraints.setReturningAttributes(attrList);

   NamingEnumeration<SearchResult> results = null;

   Object[] filterArgs = {user};
   results = ctx.search(baseDN, filter, filterArgs, constraints);
   if (!results.hasMore())
   {
      results.close();
      throw PicketBoxMessages.MESSAGES.failedToFindBaseContextDN(baseDN);
   }

   SearchResult sr = results.next();
   String name = sr.getName();
   String userDN = null;
   Attributes attrs = sr.getAttributes();
   if (attrs != null)
   {
      Attribute dn = attrs.get(distinguishedNameAttribute);
      if (dn != null)
      {
         userDN = (String) dn.get();
      }
   }
   if (userDN == null)
   {
      if (sr.isRelative())
         userDN = name + ("".equals(baseDN) ? "" : "," + baseDN);
      else
         throw PicketBoxMessages.MESSAGES.unableToFollowReferralForAuth(name);
   }

   results.close();
   results = null;
   // Bind as the user dn to authenticate the user
   InitialLdapContext userCtx = constructInitialLdapContext(userDN, credential);
   userCtx.close();

   return userDN;
}