Example usage for org.springframework.security.config.ldap LdapUserServiceBeanDefinitionParser parseAuthoritiesPopulator

List of usage examples for org.springframework.security.config.ldap LdapUserServiceBeanDefinitionParser parseAuthoritiesPopulator

Introduction

In this page you can find the example usage for org.springframework.security.config.ldap LdapUserServiceBeanDefinitionParser parseAuthoritiesPopulator.

Prototype

static RootBeanDefinition parseAuthoritiesPopulator(Element elt, ParserContext parserContext) 

Source Link

Usage

From source file:org.springframework.security.config.ldap.LdapProviderBeanDefinitionParser.java

public BeanDefinition parse(Element elt, ParserContext parserContext) {
    RuntimeBeanReference contextSource = LdapUserServiceBeanDefinitionParser.parseServerReference(elt,
            parserContext);//from www. j  a v a 2  s .com

    BeanDefinition searchBean = LdapUserServiceBeanDefinitionParser.parseSearchBean(elt, parserContext);
    String userDnPattern = elt.getAttribute(ATT_USER_DN_PATTERN);

    String[] userDnPatternArray = new String[0];

    if (StringUtils.hasText(userDnPattern)) {
        userDnPatternArray = new String[] { userDnPattern };
        // TODO: Validate the pattern and make sure it is a valid DN.
    } else if (searchBean == null) {
        logger.info("No search information or DN pattern specified. Using default search filter '"
                + DEF_USER_SEARCH_FILTER + "'");
        BeanDefinitionBuilder searchBeanBuilder = BeanDefinitionBuilder
                .rootBeanDefinition(LdapUserServiceBeanDefinitionParser.LDAP_SEARCH_CLASS);
        searchBeanBuilder.getRawBeanDefinition().setSource(elt);
        searchBeanBuilder.addConstructorArgValue("");
        searchBeanBuilder.addConstructorArgValue(DEF_USER_SEARCH_FILTER);
        searchBeanBuilder.addConstructorArgValue(contextSource);
        searchBean = searchBeanBuilder.getBeanDefinition();
    }

    BeanDefinitionBuilder authenticatorBuilder = BeanDefinitionBuilder.rootBeanDefinition(BIND_AUTH_CLASS);
    Element passwordCompareElt = DomUtils.getChildElementByTagName(elt, Elements.LDAP_PASSWORD_COMPARE);

    if (passwordCompareElt != null) {
        authenticatorBuilder = BeanDefinitionBuilder.rootBeanDefinition(PASSWD_AUTH_CLASS);

        String passwordAttribute = passwordCompareElt.getAttribute(ATT_USER_PASSWORD);
        if (StringUtils.hasText(passwordAttribute)) {
            authenticatorBuilder.addPropertyValue("passwordAttributeName", passwordAttribute);
        }

        Element passwordEncoderElement = DomUtils.getChildElementByTagName(passwordCompareElt,
                Elements.PASSWORD_ENCODER);
        String hash = passwordCompareElt.getAttribute(ATT_HASH);

        if (passwordEncoderElement != null) {
            if (StringUtils.hasText(hash)) {
                parserContext.getReaderContext().warning(
                        "Attribute 'hash' cannot be used with 'password-encoder' and " + "will be ignored.",
                        parserContext.extractSource(elt));
            }
            PasswordEncoderParser pep = new PasswordEncoderParser(passwordEncoderElement, parserContext);
            authenticatorBuilder.addPropertyValue("passwordEncoder", pep.getPasswordEncoder());
        } else if (StringUtils.hasText(hash)) {
            authenticatorBuilder.addPropertyValue("passwordEncoder",
                    PasswordEncoderParser.createPasswordEncoderBeanDefinition(hash, false));
        }
    }

    authenticatorBuilder.addConstructorArgValue(contextSource);
    authenticatorBuilder.addPropertyValue("userDnPatterns", userDnPatternArray);

    if (searchBean != null) {
        authenticatorBuilder.addPropertyValue("userSearch", searchBean);
    }

    BeanDefinitionBuilder ldapProvider = BeanDefinitionBuilder.rootBeanDefinition(PROVIDER_CLASS);
    ldapProvider.addConstructorArgValue(authenticatorBuilder.getBeanDefinition());
    ldapProvider.addConstructorArgValue(
            LdapUserServiceBeanDefinitionParser.parseAuthoritiesPopulator(elt, parserContext));
    ldapProvider.addPropertyValue("userDetailsContextMapper",
            LdapUserServiceBeanDefinitionParser.parseUserDetailsClassOrUserMapperRef(elt, parserContext));

    return ldapProvider.getBeanDefinition();
}