Search in sources :

Example 16 with DistinguishedName

use of org.springframework.ldap.core.DistinguishedName in project spring-security by spring-projects.

the class ActiveDirectoryLdapAuthenticationProvider method loadUserAuthorities.

/**
	 * Creates the user authority list from the values of the {@code memberOf} attribute
	 * obtained from the user's Active Directory entry.
	 */
@Override
protected Collection<? extends GrantedAuthority> loadUserAuthorities(DirContextOperations userData, String username, String password) {
    String[] groups = userData.getStringAttributes("memberOf");
    if (groups == null) {
        logger.debug("No values for 'memberOf' attribute.");
        return AuthorityUtils.NO_AUTHORITIES;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("'memberOf' attribute values: " + Arrays.asList(groups));
    }
    ArrayList<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(groups.length);
    for (String group : groups) {
        authorities.add(new SimpleGrantedAuthority(new DistinguishedName(group).removeLast().getValue()));
    }
    return authorities;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) DistinguishedName(org.springframework.ldap.core.DistinguishedName) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority)

Example 17 with DistinguishedName

use of org.springframework.ldap.core.DistinguishedName in project spring-security by spring-projects.

the class DefaultLdapUsernameToDnMapper method buildDn.

/**
	 * Assembles the Distinguished Name that should be used the given username.
	 */
public DistinguishedName buildDn(String username) {
    DistinguishedName dn = new DistinguishedName(userDnBase);
    dn.add(usernameAttribute, username);
    return dn;
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName)

Example 18 with DistinguishedName

use of org.springframework.ldap.core.DistinguishedName in project spring-security by spring-projects.

the class LdapUtils method getFullDn.

/**
	 * Gets the full dn of a name by prepending the name of the context it is relative to.
	 * If the name already contains the base name, it is returned unaltered.
	 */
public static DistinguishedName getFullDn(DistinguishedName dn, Context baseCtx) throws NamingException {
    DistinguishedName baseDn = new DistinguishedName(baseCtx.getNameInNamespace());
    if (dn.contains(baseDn)) {
        return dn;
    }
    baseDn.append(dn);
    return baseDn;
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName)

Example 19 with DistinguishedName

use of org.springframework.ldap.core.DistinguishedName in project spring-security by spring-projects.

the class LdapUtils method getRelativeName.

/**
	 * Obtains the part of a DN relative to a supplied base context.
	 * <p>
	 * If the DN is "cn=bob,ou=people,dc=springframework,dc=org" and the base context name
	 * is "ou=people,dc=springframework,dc=org" it would return "cn=bob".
	 * </p>
	 *
	 * @param fullDn the DN
	 * @param baseCtx the context to work out the name relative to.
	 *
	 * @return the
	 *
	 * @throws NamingException any exceptions thrown by the context are propagated.
	 */
public static String getRelativeName(String fullDn, Context baseCtx) throws NamingException {
    String baseDn = baseCtx.getNameInNamespace();
    if (baseDn.length() == 0) {
        return fullDn;
    }
    DistinguishedName base = new DistinguishedName(baseDn);
    DistinguishedName full = new DistinguishedName(fullDn);
    if (base.equals(full)) {
        return "";
    }
    Assert.isTrue(full.startsWith(base), "Full DN does not start with base DN");
    full.removeFirst(base);
    return full.toString();
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName)

Example 20 with DistinguishedName

use of org.springframework.ldap.core.DistinguishedName 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

DistinguishedName (org.springframework.ldap.core.DistinguishedName)32 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)23 Test (org.junit.Test)8 GrantedAuthority (org.springframework.security.core.GrantedAuthority)7 DirContext (javax.naming.directory.DirContext)5 BasicAttribute (javax.naming.directory.BasicAttribute)4 SearchResult (javax.naming.directory.SearchResult)4 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)4 BasicAttributes (javax.naming.directory.BasicAttributes)2 ModificationItem (javax.naming.directory.ModificationItem)2 SearchControls (javax.naming.directory.SearchControls)2 DirContextOperations (org.springframework.ldap.core.DirContextOperations)2 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 MockUserSearch (org.springframework.security.ldap.authentication.MockUserSearch)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 NamingException (javax.naming.NamingException)1