Example usage for org.springframework.security.ldap LdapUtils parseRootDnFromUrl

List of usage examples for org.springframework.security.ldap LdapUtils parseRootDnFromUrl

Introduction

In this page you can find the example usage for org.springframework.security.ldap LdapUtils parseRootDnFromUrl.

Prototype

public static String parseRootDnFromUrl(String url) 

Source Link

Document

Works out the root DN for an LDAP URL.

Usage

From source file:com.netflix.spinnaker.fiat.roles.ldap.LdapUserRolesProvider.java

private String getUserFullDn(String userId) {
    String rootDn = LdapUtils.parseRootDnFromUrl(configProps.getUrl());
    DistinguishedName root = new DistinguishedName(rootDn);
    log.debug("Root DN: " + root.toString());

    String[] formatArgs = new String[] { LdapEncoder.nameEncode(userId) };

    String partialUserDn;//from  www. j av a2s.com
    if (!StringUtils.isEmpty(configProps.getUserSearchFilter())) {
        try {
            DirContextOperations res = ldapTemplate.searchForSingleEntry(configProps.getUserSearchBase(),
                    configProps.getUserSearchFilter(), formatArgs);
            partialUserDn = res.getDn().toString();
        } catch (IncorrectResultSizeDataAccessException e) {
            log.error("Unable to find a single user entry", e);
            return null;
        }
    } else {
        partialUserDn = configProps.getUserDnPattern().format(formatArgs);
    }

    DistinguishedName user = new DistinguishedName(partialUserDn);
    log.debug("User portion: " + user.toString());

    try {
        Name fullUser = root.addAll(user);
        log.debug("Full user DN: " + fullUser.toString());
        return fullUser.toString();
    } catch (InvalidNameException ine) {
        log.error("Could not assemble full userDn", ine);
    }
    return null;
}

From source file:org.springframework.security.ldap.DefaultSpringSecurityContextSource.java

/**
 * Create and initialize an instance which will connect to the supplied LDAP URL. If
 * you want to use more than one server for fail-over, rather use the
 * {@link #DefaultSpringSecurityContextSource(List, String)} constructor.
 *
 * @param providerUrl an LDAP URL of the form
 * <code>ldap://localhost:389/base_dn</code>
 *//*  ww w  .  j a v  a2 s .  co  m*/
public DefaultSpringSecurityContextSource(String providerUrl) {
    Assert.hasLength(providerUrl, "An LDAP connection URL must be supplied.");

    StringTokenizer st = new StringTokenizer(providerUrl);

    ArrayList<String> urls = new ArrayList<>();

    // Work out rootDn from the first URL and check that the other URLs (if any) match
    while (st.hasMoreTokens()) {
        String url = st.nextToken();
        String urlRootDn = LdapUtils.parseRootDnFromUrl(url);

        urls.add(url.substring(0, url.lastIndexOf(urlRootDn)));

        this.logger.info(" URL '" + url + "', root DN is '" + urlRootDn + "'");

        if (this.rootDn == null) {
            this.rootDn = urlRootDn;
        } else if (!this.rootDn.equals(urlRootDn)) {
            throw new IllegalArgumentException("Root DNs must be the same when using multiple URLs");
        }
    }

    setUrls(urls.toArray(new String[urls.size()]));
    setBase(this.rootDn);
    setPooled(true);
    setAuthenticationStrategy(new SimpleDirContextAuthenticationStrategy() {
        @Override
        @SuppressWarnings("rawtypes")
        public void setupEnvironment(Hashtable env, String dn, String password) {
            super.setupEnvironment(env, dn, password);
            // Remove the pooling flag unless we are authenticating as the 'manager'
            // user.
            if (!DefaultSpringSecurityContextSource.this.userDn.equals(dn)
                    && env.containsKey(SUN_LDAP_POOLING_FLAG)) {
                DefaultSpringSecurityContextSource.this.logger.debug("Removing pooling flag for user " + dn);
                env.remove(SUN_LDAP_POOLING_FLAG);
            }
        }
    });
}