Example usage for org.springframework.security.ldap.search FilterBasedLdapUserSearch FilterBasedLdapUserSearch

List of usage examples for org.springframework.security.ldap.search FilterBasedLdapUserSearch FilterBasedLdapUserSearch

Introduction

In this page you can find the example usage for org.springframework.security.ldap.search FilterBasedLdapUserSearch FilterBasedLdapUserSearch.

Prototype

public FilterBasedLdapUserSearch(String searchBase, String searchFilter,
            BaseLdapPathContextSource contextSource) 

Source Link

Usage

From source file:de.interseroh.report.test.security.LdapServerTest.java

@Test
public void testJndiSpring() throws Exception {
    DefaultSpringSecurityContextSource ctxSrc = new DefaultSpringSecurityContextSource(
            "ldap://ldap.xxx:389/OU=xxx");

    ctxSrc.setUserDn(USER_LDAP);/*from  www. j  a  v a  2 s  .c  om*/
    ctxSrc.setPassword(PASSWORD_LDAP);

    ctxSrc.afterPropertiesSet();

    logger.info("Base LDAP Path: " + ctxSrc.getBaseLdapPath());
    logger.info("Principal: " + ctxSrc.getAuthenticationSource().getPrincipal().toString());
    logger.info("Credentials: " + ctxSrc.getAuthenticationSource().getCredentials());

    Authentication bob = new UsernamePasswordAuthenticationToken("bob", "bob");

    BindAuthenticator authenticator = new BindAuthenticator(ctxSrc);
    authenticator.setUserSearch(
            new FilterBasedLdapUserSearch("", "(&(objectCategory=Person)(sAMAccountName={0}))", ctxSrc));
    authenticator.afterPropertiesSet();

    authenticator.authenticate(bob);

    DirContextOperations user = authenticator.authenticate(bob);

    logger.info("User: {}", user);
}

From source file:com.evolveum.midpoint.web.boot.LdapSecurityConfig.java

@ConditionalOnProperty("auth.ldap.search.pattern")
@Bean/*from   ww  w .ja  v  a 2  s  .  c o  m*/
public FilterBasedLdapUserSearch userSearch() {
    FilterBasedLdapUserSearch search = new FilterBasedLdapUserSearch("", ldapSearchPattern, contextSource());
    search.setSearchSubtree(searchSubtree);
    return search;
}

From source file:fr.univlorraine.mondossierweb.config.SpringConfig.java

@Bean
public LdapUserSearch ldapUserSearch() {
    FilterBasedLdapUserSearch fbus = new FilterBasedLdapUserSearch("ou=people", "uid={0}", ldapServer());
    fbus.setReturningAttributes(getLdapAttributes());
    return fbus;//from w  w w  . j av a2 s .com
}

From source file:fr.univlorraine.mondossierweb.config.SpringConfig.java

@Bean
public LdapUserSearch ldapEtudiantSearch() {
    FilterBasedLdapUserSearch fbus = new FilterBasedLdapUserSearch("ou=people",
            environment.getProperty("attributLdapCodEtu") + "={0}", ldapServer());
    fbus.setReturningAttributes(getLdapAttributes());
    return fbus;/*from   w w  w.  j  a  va  2s .  c  om*/
}

From source file:net.oneandone.stool.overview.config.SecurityConfiguration.java

@Override
public UserDetailsService userDetailsService() {
    FilterBasedLdapUserSearch userSearch;
    DefaultLdapAuthoritiesPopulator authoritiesPopulator;
    LdapUserDetailsService result;//ww  w  .j  a  v a2  s.  com

    userSearch = new FilterBasedLdapUserSearch("ou=cisostages", "(uid={0})", contextSource());
    authoritiesPopulator = new DefaultLdapAuthoritiesPopulator(contextSource(), "ou=roles,ou=cisostages");
    authoritiesPopulator.setGroupSearchFilter("(member=uid={1})");
    authoritiesPopulator.setGroupRoleAttribute("ou");
    authoritiesPopulator.setSearchSubtree(false);
    authoritiesPopulator.setIgnorePartialResultException(true);

    result = new LdapUserDetailsService(userSearch, authoritiesPopulator);
    result.setUserDetailsMapper(new InetOrgPersonContextMapper());
    return result;
}

From source file:com.healthcit.cacure.businessdelegates.LdapUserManager.java

public LdapUserManager(BaseLdapPathContextSource contextSource) {
    this.contextSource = contextSource;
    users = new FilterBasedLdapUserSearch("", Constants.LDAP_USER_SEARCH_FILTER, contextSource);

}

From source file:io.gravitee.management.idp.ldap.authentication.LdapAuthenticationProviderConfigurer.java

private LdapUserSearch createUserSearch() {
    if (userSearchFilter == null) {
        return null;
    }//from w ww  .  j a  v a2  s  .  com
    return new FilterBasedLdapUserSearch(userSearchBase, userSearchFilter, contextSource);
}

From source file:org.artifactory.addon.CoreAddonsImpl.java

@Override
public List<FilterBasedLdapUserSearch> getLdapUserSearches(ContextSource ctx, LdapSetting settings) {
    SearchPattern searchPattern = settings.getSearch();
    String searchBase = searchPattern.getSearchBase();
    if (searchBase == null) {
        searchBase = "";
    }//w  w w  .  j  av  a 2  s. co  m
    ArrayList<FilterBasedLdapUserSearch> result = new ArrayList<>();
    FilterBasedLdapUserSearch userSearch = new FilterBasedLdapUserSearch(searchBase,
            searchPattern.getSearchFilter(), (BaseLdapPathContextSource) ctx);
    userSearch.setSearchSubtree(searchPattern.isSearchSubTree());
    result.add(userSearch);
    return result;
}

From source file:org.apache.atlas.web.security.AtlasADAuthenticationProvider.java

private Authentication getADBindAuthentication(Authentication authentication) {
    try {//from w w w  . j  av  a 2  s  .  c o  m
        String userName = authentication.getName();
        String userPassword = "";
        if (authentication.getCredentials() != null) {
            userPassword = authentication.getCredentials().toString();
        }

        LdapContextSource ldapContextSource = new DefaultSpringSecurityContextSource(adURL);
        ldapContextSource.setUserDn(adBindDN);
        ldapContextSource.setPassword(adBindPassword);
        ldapContextSource.setReferral(adReferral);
        ldapContextSource.setCacheEnvironmentProperties(true);
        ldapContextSource.setAnonymousReadOnly(false);
        ldapContextSource.setPooled(true);
        ldapContextSource.afterPropertiesSet();

        if (adUserSearchFilter == null || adUserSearchFilter.trim().isEmpty()) {
            adUserSearchFilter = "(sAMAccountName={0})";
        }
        FilterBasedLdapUserSearch userSearch = new FilterBasedLdapUserSearch(adBase, adUserSearchFilter,
                ldapContextSource);
        userSearch.setSearchSubtree(true);

        BindAuthenticator bindAuthenticator = new BindAuthenticator(ldapContextSource);
        bindAuthenticator.setUserSearch(userSearch);
        bindAuthenticator.afterPropertiesSet();

        LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(
                bindAuthenticator);

        if (userName != null && userPassword != null && !userName.trim().isEmpty()
                && !userPassword.trim().isEmpty()) {
            final List<GrantedAuthority> grantedAuths = getAuthorities(userName);
            final UserDetails principal = new User(userName, userPassword, grantedAuths);
            final Authentication finalAuthentication = new UsernamePasswordAuthenticationToken(principal,
                    userPassword, grantedAuths);
            authentication = ldapAuthenticationProvider.authenticate(finalAuthentication);
            if (groupsFromUGI) {
                authentication = getAuthenticationWithGrantedAuthorityFromUGI(authentication);
            }
            return authentication;
        } else {
            LOG.error("AD Authentication Failed userName or userPassword is null or empty");
            return null;
        }
    } catch (Exception e) {
        LOG.error("AD Authentication Failed:", e);
        return null;
    }
}

From source file:org.apache.atlas.web.security.AtlasLdapAuthenticationProvider.java

private Authentication getLdapBindAuthentication(Authentication authentication) {
    try {//w w  w  . j a  v a2 s  .c  om
        if (isDebugEnabled) {
            LOG.debug("==> AtlasLdapAuthenticationProvider getLdapBindAuthentication");
        }
        String userName = authentication.getName();
        String userPassword = "";
        if (authentication.getCredentials() != null) {
            userPassword = authentication.getCredentials().toString();
        }

        LdapContextSource ldapContextSource = getLdapContextSource();

        DefaultLdapAuthoritiesPopulator defaultLdapAuthoritiesPopulator = getDefaultLdapAuthoritiesPopulator(
                ldapContextSource);

        if (ldapUserSearchFilter == null || ldapUserSearchFilter.trim().isEmpty()) {
            ldapUserSearchFilter = "(uid={0})";
        }

        FilterBasedLdapUserSearch userSearch = new FilterBasedLdapUserSearch(ldapBase, ldapUserSearchFilter,
                ldapContextSource);
        userSearch.setSearchSubtree(true);

        BindAuthenticator bindAuthenticator = getBindAuthenticator(userSearch, ldapContextSource);

        LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(
                bindAuthenticator, defaultLdapAuthoritiesPopulator);

        if (userName != null && userPassword != null && !userName.trim().isEmpty()
                && !userPassword.trim().isEmpty()) {
            final List<GrantedAuthority> grantedAuths = getAuthorities(userName);
            final UserDetails principal = new User(userName, userPassword, grantedAuths);
            final Authentication finalAuthentication = new UsernamePasswordAuthenticationToken(principal,
                    userPassword, grantedAuths);
            authentication = ldapAuthenticationProvider.authenticate(finalAuthentication);
            if (groupsFromUGI) {
                authentication = getAuthenticationWithGrantedAuthorityFromUGI(authentication);
            }
            return authentication;
        } else {
            LOG.error(
                    "LDAP Authentication::userName or userPassword is null or empty for userName " + userName);
        }
    } catch (Exception e) {
        LOG.error(" getLdapBindAuthentication LDAP Authentication Failed:", e);
    }
    if (isDebugEnabled) {
        LOG.debug("<== AtlasLdapAuthenticationProvider getLdapBindAuthentication");
    }
    return authentication;
}