Example usage for org.springframework.security.ldap SpringSecurityLdapTemplate searchForSingleEntry

List of usage examples for org.springframework.security.ldap SpringSecurityLdapTemplate searchForSingleEntry

Introduction

In this page you can find the example usage for org.springframework.security.ldap SpringSecurityLdapTemplate searchForSingleEntry.

Prototype

public DirContextOperations searchForSingleEntry(final String base, final String filter,
        final Object[] params) 

Source Link

Document

Performs a search, with the requirement that the search shall return a single directory entry, and uses the supplied mapper to create the object from that entry.

Usage

From source file:com.orangeleap.common.security.OrangeLeapLdapUserSearch.java

/**
 * Return the LdapUserDetails containing the user's information
 *
 * @param username the username to search for.
 * @return An LdapUserDetails object containing the details of the located user's directory entry
 * @throws UsernameNotFoundException if no matching entry is found.
 *//*from  w ww .  j a v a2  s .  com*/
public DirContextOperations searchForUser(String username, String site) {
    SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(contextSource);

    template.setSearchControls(searchControls);

    try {
        String searchBase = "o=" + site;
        return template.searchForSingleEntry(searchBase, searchFilter, new String[] { username });

    } catch (IncorrectResultSizeDataAccessException notFound) {
        if (notFound.getActualSize() == 0) {
            throw new UsernameNotFoundException("User " + username + " not found in directory.", username);
        }
        // Search should never return multiple results if properly configured, so just rethrow
        throw notFound;
    }
}

From source file:com.orangeleap.common.security.OrangeLeapLdapUserSearch.java

/**
 * Return the LdapUserDetails containing the user's information
 *
 * @param username the username to search for.
 * @return An LdapUserDetails object containing the details of the located user's directory entry
 * @throws UsernameNotFoundException if no matching entry is found.
 *//*  w  w w .  j  av a2 s .c o  m*/
public DirContextOperations searchForUser(String username) {
    SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(contextSource);

    template.setSearchControls(searchControls);

    try {

        String[] filterArgs;
        String aSearchBase;

        if (username.indexOf("@") > -1) {
            String[] split = username.split("@");
            filterArgs = new String[] { split[0] };
            aSearchBase = "o=" + split[1];
        } else {
            filterArgs = new String[] { username };
            aSearchBase = searchBase;
        }

        return template.searchForSingleEntry(aSearchBase, searchFilter, filterArgs);

    } catch (IncorrectResultSizeDataAccessException notFound) {
        if (notFound.getActualSize() == 0) {
            throw new UsernameNotFoundException("User " + username + " not found in directory.", username);
        }
        // Search should never return multiple results if properly configured, so just rethrow
        throw notFound;
    }
}

From source file:org.springframework.security.ldap.search.FilterBasedLdapUserSearch.java

/**
 * Return the LdapUserDetails containing the user's information
 *
 * @param username the username to search for.
 *
 * @return An LdapUserDetails object containing the details of the located user's
 * directory entry//w w  w.  j a va2 s  . c o  m
 *
 * @throws UsernameNotFoundException if no matching entry is found.
 */
@Override
public DirContextOperations searchForUser(String username) {
    if (logger.isDebugEnabled()) {
        logger.debug("Searching for user '" + username + "', with user search " + this);
    }

    SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(contextSource);

    template.setSearchControls(searchControls);

    try {

        return template.searchForSingleEntry(searchBase, searchFilter, new String[] { username });

    } catch (IncorrectResultSizeDataAccessException notFound) {
        if (notFound.getActualSize() == 0) {
            throw new UsernameNotFoundException("User " + username + " not found in directory.");
        }
        // Search should never return multiple results if properly configured, so just
        // rethrow
        throw notFound;
    }
}