Search in sources :

Example 16 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-security by spring-projects.

the class AuthenticationConfigBuilder method createAccessDeniedHandler.

private BeanMetadataElement createAccessDeniedHandler(Element element, ParserContext pc) {
    Element accessDeniedElt = DomUtils.getChildElementByTagName(element, Elements.ACCESS_DENIED_HANDLER);
    BeanDefinitionBuilder accessDeniedHandler = BeanDefinitionBuilder.rootBeanDefinition(AccessDeniedHandlerImpl.class);
    if (accessDeniedElt != null) {
        String errorPage = accessDeniedElt.getAttribute("error-page");
        String ref = accessDeniedElt.getAttribute("ref");
        if (StringUtils.hasText(errorPage)) {
            if (StringUtils.hasText(ref)) {
                pc.getReaderContext().error("The attribute " + ATT_ACCESS_DENIED_ERROR_PAGE + " cannot be used together with the 'ref' attribute within <" + Elements.ACCESS_DENIED_HANDLER + ">", pc.extractSource(accessDeniedElt));
            }
            accessDeniedHandler.addPropertyValue("errorPage", errorPage);
        } else if (StringUtils.hasText(ref)) {
            return new RuntimeBeanReference(ref);
        }
    }
    return accessDeniedHandler.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 17 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-security by spring-projects.

the class AuthenticationConfigBuilder method createAnonymousFilter.

void createAnonymousFilter() {
    Element anonymousElt = DomUtils.getChildElementByTagName(httpElt, Elements.ANONYMOUS);
    if (anonymousElt != null && "false".equals(anonymousElt.getAttribute("enabled"))) {
        return;
    }
    String grantedAuthority = null;
    String username = null;
    String key = null;
    Object source = pc.extractSource(httpElt);
    if (anonymousElt != null) {
        grantedAuthority = anonymousElt.getAttribute("granted-authority");
        username = anonymousElt.getAttribute("username");
        key = anonymousElt.getAttribute(ATT_KEY);
        source = pc.extractSource(anonymousElt);
    }
    if (!StringUtils.hasText(grantedAuthority)) {
        grantedAuthority = "ROLE_ANONYMOUS";
    }
    if (!StringUtils.hasText(username)) {
        username = "anonymousUser";
    }
    if (!StringUtils.hasText(key)) {
        // Generate a random key for the Anonymous provider
        key = createKey();
    }
    anonymousFilter = new RootBeanDefinition(AnonymousAuthenticationFilter.class);
    anonymousFilter.getConstructorArgumentValues().addIndexedArgumentValue(0, key);
    anonymousFilter.getConstructorArgumentValues().addIndexedArgumentValue(1, username);
    anonymousFilter.getConstructorArgumentValues().addIndexedArgumentValue(2, AuthorityUtils.commaSeparatedStringToAuthorityList(grantedAuthority));
    anonymousFilter.setSource(source);
    RootBeanDefinition anonymousProviderBean = new RootBeanDefinition(AnonymousAuthenticationProvider.class);
    anonymousProviderBean.getConstructorArgumentValues().addIndexedArgumentValue(0, key);
    anonymousProviderBean.setSource(anonymousFilter.getSource());
    String id = pc.getReaderContext().generateBeanName(anonymousProviderBean);
    pc.registerBeanComponent(new BeanComponentDefinition(anonymousProviderBean, id));
    anonymousProviderRef = new RuntimeBeanReference(id);
}
Also used : AnonymousAuthenticationFilter(org.springframework.security.web.authentication.AnonymousAuthenticationFilter) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 18 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-security by spring-projects.

the class AuthenticationConfigBuilder method createBasicFilter.

void createBasicFilter(BeanReference authManager) {
    Element basicAuthElt = DomUtils.getChildElementByTagName(httpElt, Elements.BASIC_AUTH);
    if (basicAuthElt == null && !autoConfig) {
        // No basic auth, do nothing
        return;
    }
    String realm = httpElt.getAttribute(ATT_REALM);
    if (!StringUtils.hasText(realm)) {
        realm = DEF_REALM;
    }
    BeanDefinitionBuilder filterBuilder = BeanDefinitionBuilder.rootBeanDefinition(BasicAuthenticationFilter.class);
    String entryPointId;
    if (basicAuthElt != null) {
        if (StringUtils.hasText(basicAuthElt.getAttribute(ATT_ENTRY_POINT_REF))) {
            basicEntryPoint = new RuntimeBeanReference(basicAuthElt.getAttribute(ATT_ENTRY_POINT_REF));
        }
        injectAuthenticationDetailsSource(basicAuthElt, filterBuilder);
    }
    if (basicEntryPoint == null) {
        RootBeanDefinition entryPoint = new RootBeanDefinition(BasicAuthenticationEntryPoint.class);
        entryPoint.setSource(pc.extractSource(httpElt));
        entryPoint.getPropertyValues().addPropertyValue("realmName", realm);
        entryPointId = pc.getReaderContext().generateBeanName(entryPoint);
        pc.registerBeanComponent(new BeanComponentDefinition(entryPoint, entryPointId));
        basicEntryPoint = new RuntimeBeanReference(entryPointId);
    }
    filterBuilder.addConstructorArgValue(authManager);
    filterBuilder.addConstructorArgValue(basicEntryPoint);
    basicFilter = filterBuilder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 19 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-security by spring-projects.

the class CorsBeanDefinitionParser method parse.

public BeanMetadataElement parse(Element element, ParserContext parserContext) {
    if (element == null) {
        return null;
    }
    String filterRef = element.getAttribute(ATT_REF);
    if (StringUtils.hasText(filterRef)) {
        return new RuntimeBeanReference(filterRef);
    }
    BeanMetadataElement configurationSource = getSource(element, parserContext);
    if (configurationSource == null) {
        throw new BeanCreationException("Could not create CorsFilter");
    }
    BeanDefinitionBuilder filterBldr = BeanDefinitionBuilder.rootBeanDefinition(CorsFilter.class);
    filterBldr.addConstructorArgValue(configurationSource);
    return filterBldr.getBeanDefinition();
}
Also used : BeanMetadataElement(org.springframework.beans.BeanMetadataElement) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 20 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-security by spring-projects.

the class ClearCredentialsMethodInvokingFactoryBean method buildCustomFilterList.

List<OrderDecorator> buildCustomFilterList(Element element, ParserContext pc) {
    List<Element> customFilterElts = DomUtils.getChildElementsByTagName(element, Elements.CUSTOM_FILTER);
    List<OrderDecorator> customFilters = new ArrayList<OrderDecorator>();
    final String ATT_AFTER = "after";
    final String ATT_BEFORE = "before";
    final String ATT_POSITION = "position";
    for (Element elt : customFilterElts) {
        String after = elt.getAttribute(ATT_AFTER);
        String before = elt.getAttribute(ATT_BEFORE);
        String position = elt.getAttribute(ATT_POSITION);
        String ref = elt.getAttribute(ATT_REF);
        if (!StringUtils.hasText(ref)) {
            pc.getReaderContext().error("The '" + ATT_REF + "' attribute must be supplied", pc.extractSource(elt));
        }
        RuntimeBeanReference bean = new RuntimeBeanReference(ref);
        if (WebConfigUtils.countNonEmpty(new String[] { after, before, position }) != 1) {
            pc.getReaderContext().error("A single '" + ATT_AFTER + "', '" + ATT_BEFORE + "', or '" + ATT_POSITION + "' attribute must be supplied", pc.extractSource(elt));
        }
        if (StringUtils.hasText(position)) {
            customFilters.add(new OrderDecorator(bean, SecurityFilters.valueOf(position)));
        } else if (StringUtils.hasText(after)) {
            SecurityFilters order = SecurityFilters.valueOf(after);
            if (order == SecurityFilters.LAST) {
                customFilters.add(new OrderDecorator(bean, SecurityFilters.LAST));
            } else {
                customFilters.add(new OrderDecorator(bean, order.getOrder() + 1));
            }
        } else if (StringUtils.hasText(before)) {
            SecurityFilters order = SecurityFilters.valueOf(before);
            if (order == SecurityFilters.FIRST) {
                customFilters.add(new OrderDecorator(bean, SecurityFilters.FIRST));
            } else {
                customFilters.add(new OrderDecorator(bean, order.getOrder() - 1));
            }
        }
    }
    return customFilters;
}
Also used : BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Aggregations

RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)156 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)86 Element (org.w3c.dom.Element)47 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)39 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)33 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)32 ManagedList (org.springframework.beans.factory.support.ManagedList)27 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)24 Test (org.junit.Test)21 ManagedMap (org.springframework.beans.factory.support.ManagedMap)20 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)16 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)15 GroovyObject (groovy.lang.GroovyObject)12 TestBean (org.springframework.tests.sample.beans.TestBean)12 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)11 Map (java.util.Map)10 Node (org.w3c.dom.Node)10 HashMap (java.util.HashMap)9 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)9 ITestBean (org.springframework.tests.sample.beans.ITestBean)8