下面列出了javax.naming.ldap.InitialLdapContext#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
protected void safeClose(InitialLdapContext ic)
{
if(ic != null)
{
try
{
ic.close();
}
catch (NamingException e)
{
}
}
}
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;
}