Package org.springframework.ldap.core

Examples of org.springframework.ldap.core.DirContextOperations


        authenticateIfNeeded(new AuthenticatedLdapEntryContextCallback() {
           
            @Override
            public void executeWithContext(DirContext ctx,
                    LdapEntryIdentification ldapEntryIdentification) {
                DirContextOperations obj = (DirContextOperations)LDAPUtils
                        .getLdapTemplateInContext(ctx, template)
                        .lookup(user);
                String name = obj.getObjectAttribute(userNameAttribute).toString();
                Matcher m = userNamePattern.matcher(name);
                if(m.matches()) {
                    name = m.group(1);
                }
                userName.clear();
View Full Code Here


     * @param authentication
     * @return
     */
    protected DirContextOperations authenticateUsingFilter(
            Authentication authentication) {
        DirContextOperations user = null;
        Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class,
                authentication,
                "Can only process UsernamePasswordAuthenticationToken objects");

        String username = authentication.getName();
        String originalUser = username;
        String password = (String) authentication.getCredentials();
        // format given username if required
        if (userFormat != null && !userFormat.equals("")) {
            username = MessageFormat.format(userFormat, username);
        }
        if (!StringUtils.hasLength(password)) {
            logger.debug("Rejecting empty password for user " + username);
            throw new BadCredentialsException(messages.getMessage(
                    "BindAuthenticator.emptyPassword", "Empty Password"));
        }

        DirContext ctx = null;
        String userDnStr = "";
        try {
            ctx = getContextSource().getContext(username, password);

            // Check for password policy control
            PasswordPolicyControl ppolicy = PasswordPolicyControlExtractor
                    .extractControl(ctx);

            logger.debug("Retrieving user object using filter...");
            SearchControls searchCtls = new SearchControls();
            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

            user = SpringSecurityLdapTemplate.searchForSingleEntryInternal(ctx,
                    searchCtls, "", userFilter, new Object[] { username, originalUser });
            userDnStr = user.getDn().toString();
            if (ppolicy != null) {
                user.setAttributeValue(ppolicy.getID(), ppolicy);
            }

        } catch (NamingException e) {
            // This will be thrown if an invalid user name is used and the
            // method may
View Full Code Here

        AttributeDef attrDef = configuration.getAttributeDef(attributeName);
        if (attrDef == null)
        {
            throw new SecurityException(SecurityException.ENTITY_ATTRIBUTE_UNDEFINED.createScoped(configuration.getEntityType(), attributeName));
        }
        DirContextOperations dirCtxOps = getEntityContextById(entityId, false);
        if (dirCtxOps == null)
        {
            throw new SecurityException(SecurityException.PRINCIPAL_DOES_NOT_EXIST.createScoped(configuration.getEntityType(), entityId));
        }
        ModificationItem[] modItems = new ModificationItem[1];
        modItems[0] = new ModificationItem(attrDef.isMultiValue() ? DirContext.ADD_ATTRIBUTE : DirContext.REPLACE_ATTRIBUTE, new BasicAttribute(attributeName));
        modItems[0].getAttribute().add(relatedEntityInternalId);
       
        ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
        try
        {
            Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
            ldapTemplate.modifyAttributes(getRelativeDN(dirCtxOps.getNameInNamespace()), modItems);
        }
        catch (AttributeInUseException e)
        {
            // relation already defined, ignore
        }
View Full Code Here

        AttributeDef attrDef = configuration.getAttributeDef(attributeName);
        if (attrDef == null)
        {
            throw new SecurityException(SecurityException.ENTITY_ATTRIBUTE_UNDEFINED.createScoped(configuration.getEntityType(), attributeName));
        }
        DirContextOperations dirCtxOps = getEntityContextById(entityId, false);
        if (dirCtxOps == null)
        {
            throw new SecurityException(SecurityException.PRINCIPAL_DOES_NOT_EXIST.createScoped(configuration.getEntityType(), entityId));
        }
        ModificationItem[] modItems = new ModificationItem[1];
        modItems[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute(attributeName));
        if (attrDef.isMultiValue())
        {
            modItems[0].getAttribute().add(relatedEntityInternalId);
        }
       
        ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
        try
        {
            Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
            try
            {
                ldapTemplate.modifyAttributes(getRelativeDN(dirCtxOps.getNameInNamespace()), modItems);
            }
            catch (SchemaViolationException e)
            {
                // required multi-value attribute removal?
                if (!(attrDef.isMultiValue() && attrDef.isRequired()))
                {
                    throw e;
                }
                // replace with required default or dn
                modItems[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new  BasicAttribute(attributeName));
                modItems[0].getAttribute().add(attrDef.requiresDnDefaultValue() ? dirCtxOps.getNameInNamespace() : attrDef.getRequiredDefaultValue());
                // try again
                ldapTemplate.modifyAttributes(getRelativeDN(dirCtxOps.getNameInNamespace()), modItems);
            }
        }
        catch (NamingException e)
        {
            throw new SecurityException(SecurityException.UNEXPECTED.create(getClass().getName(), "removeRelation", e.getMessage()), e);
View Full Code Here

    }
   
    public Entity loadEntity(Object providerContext)
    {
        Entity entity = null;
        DirContextOperations ctx = null;
       
        if (providerContext instanceof SearchResult)
        {
            ctx = (DirContextOperations) ((SearchResult) (providerContext)).getObject();
        }
        else if (providerContext instanceof DirContextAdapter)
        {
            ctx = (DirContextOperations) providerContext;
        }
        if (ctx != null)
        {
            String entityId = null;
            String dn = ctx.getNameInNamespace();
            Set<Attribute> attributes = new HashSet<Attribute>();
            Attributes attrs = ctx.getAttributes();
            for (AttributeDef attrDef : searchConfiguration.getEntityAttributeDefinitionsMap().values())
            {
                List<String> values = null;
                values = getStringAttributes(attrs, attrDef.getName(), attrDef.requiresDnDefaultValue());
                if (values != null)
View Full Code Here

TOP

Related Classes of org.springframework.ldap.core.DirContextOperations

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.