Search in sources :

Example 6 with IncorrectResultSizeDataAccessException

use of org.springframework.dao.IncorrectResultSizeDataAccessException in project spring-security by spring-projects.

the class SpringSecurityLdapTemplate method searchForSingleEntryInternal.

/**
	 * Internal method extracted to avoid code duplication in AD search.
	 */
public static DirContextOperations searchForSingleEntryInternal(DirContext ctx, SearchControls searchControls, String base, String filter, Object[] params) throws NamingException {
    final DistinguishedName ctxBaseDn = new DistinguishedName(ctx.getNameInNamespace());
    final DistinguishedName searchBaseDn = new DistinguishedName(base);
    final NamingEnumeration<SearchResult> resultsEnum = ctx.search(searchBaseDn, filter, params, buildControls(searchControls));
    if (logger.isDebugEnabled()) {
        logger.debug("Searching for entry under DN '" + ctxBaseDn + "', base = '" + searchBaseDn + "', filter = '" + filter + "'");
    }
    Set<DirContextOperations> results = new HashSet<DirContextOperations>();
    try {
        while (resultsEnum.hasMore()) {
            SearchResult searchResult = resultsEnum.next();
            DirContextAdapter dca = (DirContextAdapter) searchResult.getObject();
            Assert.notNull(dca, "No object returned by search, DirContext is not correctly configured");
            if (logger.isDebugEnabled()) {
                logger.debug("Found DN: " + dca.getDn());
            }
            results.add(dca);
        }
    } catch (PartialResultException e) {
        LdapUtils.closeEnumeration(resultsEnum);
        logger.info("Ignoring PartialResultException");
    }
    if (results.size() == 0) {
        throw new IncorrectResultSizeDataAccessException(1, 0);
    }
    if (results.size() > 1) {
        throw new IncorrectResultSizeDataAccessException(1, results.size());
    }
    return results.iterator().next();
}
Also used : DirContextOperations(org.springframework.ldap.core.DirContextOperations) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) SearchResult(javax.naming.directory.SearchResult) PartialResultException(javax.naming.PartialResultException) HashSet(java.util.HashSet)

Aggregations

IncorrectResultSizeDataAccessException (org.springframework.dao.IncorrectResultSizeDataAccessException)6 GeneralSecurityException (java.security.GeneralSecurityException)2 AccountNotFoundException (javax.security.auth.login.AccountNotFoundException)2 FailedLoginException (javax.security.auth.login.FailedLoginException)2 PreventedException (org.apereo.cas.authentication.PreventedException)2 AccountDisabledException (org.apereo.cas.authentication.exceptions.AccountDisabledException)2 AccountPasswordMustChangeException (org.apereo.cas.authentication.exceptions.AccountPasswordMustChangeException)2 DataAccessException (org.springframework.dao.DataAccessException)2 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)2 Pair (com.alibaba.cobar.manager.util.Pair)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 PartialResultException (javax.naming.PartialResultException)1 SearchControls (javax.naming.directory.SearchControls)1 SearchResult (javax.naming.directory.SearchResult)1 StatementCallback (org.springframework.jdbc.core.StatementCallback)1 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)1 DirContextOperations (org.springframework.ldap.core.DirContextOperations)1