Search in sources :

Example 6 with ManagedList

use of org.springframework.beans.factory.support.ManagedList in project spring-security by spring-projects.

the class FilterChainBeanDefinitionParser method parse.

public BeanDefinition parse(Element elt, ParserContext pc) {
    MatcherType matcherType = MatcherType.fromElement(elt);
    String path = elt.getAttribute(HttpSecurityBeanDefinitionParser.ATT_PATH_PATTERN);
    String requestMatcher = elt.getAttribute(ATT_REQUEST_MATCHER_REF);
    String filters = elt.getAttribute(HttpSecurityBeanDefinitionParser.ATT_FILTERS);
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DefaultSecurityFilterChain.class);
    if (StringUtils.hasText(path)) {
        Assert.isTrue(!StringUtils.hasText(requestMatcher), "");
        builder.addConstructorArgValue(matcherType.createMatcher(pc, path, null));
    } else {
        Assert.isTrue(StringUtils.hasText(requestMatcher), "");
        builder.addConstructorArgReference(requestMatcher);
    }
    if (filters.equals(HttpSecurityBeanDefinitionParser.OPT_FILTERS_NONE)) {
        builder.addConstructorArgValue(Collections.EMPTY_LIST);
    } else {
        String[] filterBeanNames = StringUtils.tokenizeToStringArray(filters, ",");
        ManagedList<RuntimeBeanReference> filterChain = new ManagedList<RuntimeBeanReference>(filterBeanNames.length);
        for (String name : filterBeanNames) {
            filterChain.add(new RuntimeBeanReference(name));
        }
        builder.addConstructorArgValue(filterChain);
    }
    return builder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ManagedList(org.springframework.beans.factory.support.ManagedList) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 7 with ManagedList

use of org.springframework.beans.factory.support.ManagedList in project spring-security by spring-projects.

the class FilterChainMapBeanDefinitionDecorator method decorate.

@SuppressWarnings("unchecked")
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder holder, ParserContext parserContext) {
    BeanDefinition filterChainProxy = holder.getBeanDefinition();
    ManagedList<BeanMetadataElement> securityFilterChains = new ManagedList<BeanMetadataElement>();
    Element elt = (Element) node;
    MatcherType matcherType = MatcherType.fromElement(elt);
    List<Element> filterChainElts = DomUtils.getChildElementsByTagName(elt, Elements.FILTER_CHAIN);
    for (Element chain : filterChainElts) {
        String path = chain.getAttribute(HttpSecurityBeanDefinitionParser.ATT_PATH_PATTERN);
        String filters = chain.getAttribute(HttpSecurityBeanDefinitionParser.ATT_FILTERS);
        if (!StringUtils.hasText(path)) {
            parserContext.getReaderContext().error("The attribute '" + HttpSecurityBeanDefinitionParser.ATT_PATH_PATTERN + "' must not be empty", elt);
        }
        if (!StringUtils.hasText(filters)) {
            parserContext.getReaderContext().error("The attribute '" + HttpSecurityBeanDefinitionParser.ATT_FILTERS + "'must not be empty", elt);
        }
        BeanDefinition matcher = matcherType.createMatcher(parserContext, path, null);
        if (filters.equals(HttpSecurityBeanDefinitionParser.OPT_FILTERS_NONE)) {
            securityFilterChains.add(createSecurityFilterChain(matcher, new ManagedList(0)));
        } else {
            String[] filterBeanNames = StringUtils.tokenizeToStringArray(filters, ",");
            ManagedList filterChain = new ManagedList(filterBeanNames.length);
            for (String name : filterBeanNames) {
                filterChain.add(new RuntimeBeanReference(name));
            }
            securityFilterChains.add(createSecurityFilterChain(matcher, filterChain));
        }
    }
    filterChainProxy.getConstructorArgumentValues().addGenericArgumentValue(securityFilterChains);
    return holder;
}
Also used : BeanMetadataElement(org.springframework.beans.BeanMetadataElement) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 8 with ManagedList

use of org.springframework.beans.factory.support.ManagedList in project spring-security by spring-projects.

the class AuthenticationConfigBuilder method getLogoutHandlers.

@SuppressWarnings({ "rawtypes", "unchecked" })
ManagedList getLogoutHandlers() {
    if (logoutHandlers == null && rememberMeProviderRef != null) {
        logoutHandlers = new ManagedList();
        if (csrfLogoutHandler != null) {
            logoutHandlers.add(csrfLogoutHandler);
        }
        logoutHandlers.add(new RuntimeBeanReference(rememberMeServicesId));
        logoutHandlers.add(new RootBeanDefinition(SecurityContextLogoutHandler.class));
    }
    return logoutHandlers;
}
Also used : SecurityContextLogoutHandler(org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 9 with ManagedList

use of org.springframework.beans.factory.support.ManagedList in project spring-security by spring-projects.

the class UserServiceBeanDefinitionParser method doParse.

@SuppressWarnings("unchecked")
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    String userProperties = element.getAttribute(ATT_PROPERTIES);
    List<Element> userElts = DomUtils.getChildElementsByTagName(element, ELT_USER);
    if (StringUtils.hasText(userProperties)) {
        if (!CollectionUtils.isEmpty(userElts)) {
            throw new BeanDefinitionStoreException("Use of a properties file and user elements are mutually exclusive");
        }
        BeanDefinition bd = new RootBeanDefinition(PropertiesFactoryBean.class);
        bd.getPropertyValues().addPropertyValue("location", userProperties);
        builder.addConstructorArgValue(bd);
        return;
    }
    if (CollectionUtils.isEmpty(userElts)) {
        throw new BeanDefinitionStoreException("You must supply user definitions, either with <" + ELT_USER + "> child elements or a " + "properties file (using the '" + ATT_PROPERTIES + "' attribute)");
    }
    ManagedList<BeanDefinition> users = new ManagedList<BeanDefinition>();
    for (Object elt : userElts) {
        Element userElt = (Element) elt;
        String userName = userElt.getAttribute(ATT_NAME);
        String password = userElt.getAttribute(ATT_PASSWORD);
        if (!StringUtils.hasLength(password)) {
            password = generateRandomPassword();
        }
        boolean locked = "true".equals(userElt.getAttribute(ATT_LOCKED));
        boolean disabled = "true".equals(userElt.getAttribute(ATT_DISABLED));
        BeanDefinitionBuilder authorities = BeanDefinitionBuilder.rootBeanDefinition(AuthorityUtils.class);
        authorities.addConstructorArgValue(userElt.getAttribute(ATT_AUTHORITIES));
        authorities.setFactoryMethod("commaSeparatedStringToAuthorityList");
        BeanDefinitionBuilder user = BeanDefinitionBuilder.rootBeanDefinition(User.class);
        user.addConstructorArgValue(userName);
        user.addConstructorArgValue(password);
        user.addConstructorArgValue(!disabled);
        user.addConstructorArgValue(true);
        user.addConstructorArgValue(true);
        user.addConstructorArgValue(!locked);
        user.addConstructorArgValue(authorities.getBeanDefinition());
        users.add(user.getBeanDefinition());
    }
    builder.addConstructorArgValue(users);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 10 with ManagedList

use of org.springframework.beans.factory.support.ManagedList in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testPrototypeWithArrayConversionForConstructor.

@Test
public void testPrototypeWithArrayConversionForConstructor() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    List<String> list = new ManagedList<>();
    list.add("myName");
    list.add("myBeanName");
    RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
    bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    bd.getConstructorArgumentValues().addGenericArgumentValue(list);
    lbf.registerBeanDefinition("test", bd);
    DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test");
    assertEquals("myName", tb.getName());
    assertEquals("myBeanName", tb.getBeanName());
    DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test");
    assertTrue(tb != tb2);
    assertEquals("myName", tb2.getName());
    assertEquals("myBeanName", tb2.getBeanName());
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Aggregations

ManagedList (org.springframework.beans.factory.support.ManagedList)60 Element (org.w3c.dom.Element)37 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)27 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)26 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)20 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)18 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)10 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)10 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)6 Node (org.w3c.dom.Node)6 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)5 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)4 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)4 GroovyObject (groovy.lang.GroovyObject)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BeanReference (org.springframework.beans.factory.config.BeanReference)3 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)3 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)3 ManagedMap (org.springframework.beans.factory.support.ManagedMap)3