Search in sources :

Example 6 with BeanReference

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

the class GlobalMethodSecurityBeanDefinitionParser method parse.

public BeanDefinition parse(Element element, ParserContext pc) {
    CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), pc.extractSource(element));
    pc.pushContainingComponent(compositeDef);
    Object source = pc.extractSource(element);
    // The list of method metadata delegates
    ManagedList<BeanMetadataElement> delegates = new ManagedList<BeanMetadataElement>();
    boolean jsr250Enabled = "enabled".equals(element.getAttribute(ATT_USE_JSR250));
    boolean useSecured = "enabled".equals(element.getAttribute(ATT_USE_SECURED));
    boolean prePostAnnotationsEnabled = "enabled".equals(element.getAttribute(ATT_USE_PREPOST));
    boolean useAspectJ = "aspectj".equals(element.getAttribute(ATT_MODE));
    BeanDefinition preInvocationVoter = null;
    ManagedList<BeanMetadataElement> afterInvocationProviders = new ManagedList<BeanMetadataElement>();
    // Check for an external SecurityMetadataSource, which takes priority over other
    // sources
    String metaDataSourceId = element.getAttribute(ATT_META_DATA_SOURCE_REF);
    if (StringUtils.hasText(metaDataSourceId)) {
        delegates.add(new RuntimeBeanReference(metaDataSourceId));
    }
    if (prePostAnnotationsEnabled) {
        Element prePostElt = DomUtils.getChildElementByTagName(element, INVOCATION_HANDLING);
        Element expressionHandlerElt = DomUtils.getChildElementByTagName(element, EXPRESSION_HANDLER);
        if (prePostElt != null && expressionHandlerElt != null) {
            pc.getReaderContext().error(INVOCATION_HANDLING + " and " + EXPRESSION_HANDLER + " cannot be used together ", source);
        }
        BeanDefinitionBuilder preInvocationVoterBldr = BeanDefinitionBuilder.rootBeanDefinition(PreInvocationAuthorizationAdviceVoter.class);
        // After-invocation provider to handle post-invocation filtering and
        // authorization expression annotations.
        BeanDefinitionBuilder afterInvocationBldr = BeanDefinitionBuilder.rootBeanDefinition(PostInvocationAdviceProvider.class);
        // The metadata source for the security interceptor
        BeanDefinitionBuilder mds = BeanDefinitionBuilder.rootBeanDefinition(PrePostAnnotationSecurityMetadataSource.class);
        if (prePostElt != null) {
            // Customized override of expression handling system
            String attributeFactoryRef = DomUtils.getChildElementByTagName(prePostElt, INVOCATION_ATTRIBUTE_FACTORY).getAttribute("ref");
            String preAdviceRef = DomUtils.getChildElementByTagName(prePostElt, PRE_INVOCATION_ADVICE).getAttribute("ref");
            String postAdviceRef = DomUtils.getChildElementByTagName(prePostElt, POST_INVOCATION_ADVICE).getAttribute("ref");
            mds.addConstructorArgReference(attributeFactoryRef);
            preInvocationVoterBldr.addConstructorArgReference(preAdviceRef);
            afterInvocationBldr.addConstructorArgReference(postAdviceRef);
        } else {
            // The default expression-based system
            String expressionHandlerRef = expressionHandlerElt == null ? null : expressionHandlerElt.getAttribute("ref");
            if (StringUtils.hasText(expressionHandlerRef)) {
                logger.info("Using bean '" + expressionHandlerRef + "' as method ExpressionHandler implementation");
                RootBeanDefinition lazyInitPP = new RootBeanDefinition(LazyInitBeanDefinitionRegistryPostProcessor.class);
                lazyInitPP.getConstructorArgumentValues().addGenericArgumentValue(expressionHandlerRef);
                pc.getReaderContext().registerWithGeneratedName(lazyInitPP);
                BeanDefinitionBuilder lazyMethodSecurityExpressionHandlerBldr = BeanDefinitionBuilder.rootBeanDefinition(LazyInitTargetSource.class);
                lazyMethodSecurityExpressionHandlerBldr.addPropertyValue("targetBeanName", expressionHandlerRef);
                BeanDefinitionBuilder expressionHandlerProxyBldr = BeanDefinitionBuilder.rootBeanDefinition(ProxyFactoryBean.class);
                expressionHandlerProxyBldr.addPropertyValue("targetSource", lazyMethodSecurityExpressionHandlerBldr.getBeanDefinition());
                expressionHandlerProxyBldr.addPropertyValue("proxyInterfaces", MethodSecurityExpressionHandler.class);
                expressionHandlerRef = pc.getReaderContext().generateBeanName(expressionHandlerProxyBldr.getBeanDefinition());
                pc.registerBeanComponent(new BeanComponentDefinition(expressionHandlerProxyBldr.getBeanDefinition(), expressionHandlerRef));
            } else {
                RootBeanDefinition expressionHandler = registerWithDefaultRolePrefix(pc, DefaultMethodSecurityExpressionHandlerBeanFactory.class);
                expressionHandlerRef = pc.getReaderContext().generateBeanName(expressionHandler);
                pc.registerBeanComponent(new BeanComponentDefinition(expressionHandler, expressionHandlerRef));
                logger.info("Expressions were enabled for method security but no SecurityExpressionHandler was configured. " + "All hasPermision() expressions will evaluate to false.");
            }
            BeanDefinitionBuilder expressionPreAdviceBldr = BeanDefinitionBuilder.rootBeanDefinition(ExpressionBasedPreInvocationAdvice.class);
            expressionPreAdviceBldr.addPropertyReference("expressionHandler", expressionHandlerRef);
            preInvocationVoterBldr.addConstructorArgValue(expressionPreAdviceBldr.getBeanDefinition());
            BeanDefinitionBuilder expressionPostAdviceBldr = BeanDefinitionBuilder.rootBeanDefinition(ExpressionBasedPostInvocationAdvice.class);
            expressionPostAdviceBldr.addConstructorArgReference(expressionHandlerRef);
            afterInvocationBldr.addConstructorArgValue(expressionPostAdviceBldr.getBeanDefinition());
            BeanDefinitionBuilder annotationInvocationFactory = BeanDefinitionBuilder.rootBeanDefinition(ExpressionBasedAnnotationAttributeFactory.class);
            annotationInvocationFactory.addConstructorArgReference(expressionHandlerRef);
            mds.addConstructorArgValue(annotationInvocationFactory.getBeanDefinition());
        }
        preInvocationVoter = preInvocationVoterBldr.getBeanDefinition();
        afterInvocationProviders.add(afterInvocationBldr.getBeanDefinition());
        delegates.add(mds.getBeanDefinition());
    }
    if (useSecured) {
        delegates.add(BeanDefinitionBuilder.rootBeanDefinition(SecuredAnnotationSecurityMetadataSource.class).getBeanDefinition());
    }
    if (jsr250Enabled) {
        RootBeanDefinition jsrMetadataSource = registerWithDefaultRolePrefix(pc, Jsr250MethodSecurityMetadataSourceBeanFactory.class);
        delegates.add(jsrMetadataSource);
    }
    // Now create a Map<String, ConfigAttribute> for each <protect-pointcut>
    // sub-element
    Map<String, List<ConfigAttribute>> pointcutMap = parseProtectPointcuts(pc, DomUtils.getChildElementsByTagName(element, PROTECT_POINTCUT));
    if (pointcutMap.size() > 0) {
        if (useAspectJ) {
            pc.getReaderContext().error("You can't use AspectJ mode with protect-pointcut definitions", source);
        }
        // Only add it if there are actually any pointcuts defined.
        BeanDefinition mapBasedMetadataSource = new RootBeanDefinition(MapBasedMethodSecurityMetadataSource.class);
        BeanReference ref = new RuntimeBeanReference(pc.getReaderContext().generateBeanName(mapBasedMetadataSource));
        delegates.add(ref);
        pc.registerBeanComponent(new BeanComponentDefinition(mapBasedMetadataSource, ref.getBeanName()));
        registerProtectPointcutPostProcessor(pc, pointcutMap, ref, source);
    }
    BeanReference metadataSource = registerDelegatingMethodSecurityMetadataSource(pc, delegates, source);
    // Check for additional after-invocation-providers..
    List<Element> afterInvocationElts = DomUtils.getChildElementsByTagName(element, Elements.AFTER_INVOCATION_PROVIDER);
    for (Element elt : afterInvocationElts) {
        afterInvocationProviders.add(new RuntimeBeanReference(elt.getAttribute(ATT_REF)));
    }
    String accessManagerId = element.getAttribute(ATT_ACCESS_MGR);
    if (!StringUtils.hasText(accessManagerId)) {
        accessManagerId = registerAccessManager(pc, jsr250Enabled, preInvocationVoter);
    }
    String authMgrRef = element.getAttribute(ATT_AUTHENTICATION_MANAGER_REF);
    String runAsManagerId = element.getAttribute(ATT_RUN_AS_MGR);
    BeanReference interceptor = registerMethodSecurityInterceptor(pc, authMgrRef, accessManagerId, runAsManagerId, metadataSource, afterInvocationProviders, source, useAspectJ);
    if (useAspectJ) {
        BeanDefinitionBuilder aspect = BeanDefinitionBuilder.rootBeanDefinition("org.springframework.security.access.intercept.aspectj.aspect.AnnotationSecurityAspect");
        aspect.setFactoryMethod("aspectOf");
        aspect.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        aspect.addPropertyValue("securityInterceptor", interceptor);
        String id = pc.getReaderContext().registerWithGeneratedName(aspect.getBeanDefinition());
        pc.registerBeanComponent(new BeanComponentDefinition(aspect.getBeanDefinition(), id));
    } else {
        registerAdvisor(pc, interceptor, metadataSource, source, element.getAttribute(ATT_ADVICE_ORDER));
        AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(pc, element);
    }
    pc.popAndRegisterContainingComponent();
    return null;
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) List(java.util.List) ArrayList(java.util.ArrayList) BeanReference(org.springframework.beans.factory.config.BeanReference) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 7 with BeanReference

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

the class ConfigUtils method findDefaultFilterChainBeanId.

@SuppressWarnings({ "unchecked" })
protected static String findDefaultFilterChainBeanId(ParserContext parserContext) {
    BeanDefinition filterChainList = parserContext.getRegistry().getBeanDefinition(BeanIds.FILTER_CHAINS);
    // Get the list of SecurityFilterChain beans
    List<BeanReference> filterChains = (List<BeanReference>) filterChainList.getPropertyValues().getPropertyValue("sourceList").getValue();
    return filterChains.get(filterChains.size() - 1).getBeanName();
}
Also used : List(java.util.List) BeanReference(org.springframework.beans.factory.config.BeanReference) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 8 with BeanReference

use of org.springframework.beans.factory.config.BeanReference in project spring-framework by spring-projects.

the class BeanComponentDefinition method findInnerBeanDefinitionsAndBeanReferences.

private void findInnerBeanDefinitionsAndBeanReferences(BeanDefinition beanDefinition) {
    List<BeanDefinition> innerBeans = new ArrayList<>();
    List<BeanReference> references = new ArrayList<>();
    PropertyValues propertyValues = beanDefinition.getPropertyValues();
    for (int i = 0; i < propertyValues.getPropertyValues().length; i++) {
        PropertyValue propertyValue = propertyValues.getPropertyValues()[i];
        Object value = propertyValue.getValue();
        if (value instanceof BeanDefinitionHolder) {
            innerBeans.add(((BeanDefinitionHolder) value).getBeanDefinition());
        } else if (value instanceof BeanDefinition) {
            innerBeans.add((BeanDefinition) value);
        } else if (value instanceof BeanReference) {
            references.add((BeanReference) value);
        }
    }
    this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[innerBeans.size()]);
    this.beanReferences = references.toArray(new BeanReference[references.size()]);
}
Also used : PropertyValues(org.springframework.beans.PropertyValues) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) ArrayList(java.util.ArrayList) PropertyValue(org.springframework.beans.PropertyValue) BeanReference(org.springframework.beans.factory.config.BeanReference) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 9 with BeanReference

use of org.springframework.beans.factory.config.BeanReference in project spring-framework by spring-projects.

the class AdvisorComponentDefinition method unwrapDefinitions.

private void unwrapDefinitions(BeanDefinition advisorDefinition, BeanDefinition pointcutDefinition) {
    MutablePropertyValues pvs = advisorDefinition.getPropertyValues();
    BeanReference adviceReference = (BeanReference) pvs.getPropertyValue("adviceBeanName").getValue();
    if (pointcutDefinition != null) {
        this.beanReferences = new BeanReference[] { adviceReference };
        this.beanDefinitions = new BeanDefinition[] { advisorDefinition, pointcutDefinition };
        this.description = buildDescription(adviceReference, pointcutDefinition);
    } else {
        BeanReference pointcutReference = (BeanReference) pvs.getPropertyValue("pointcut").getValue();
        this.beanReferences = new BeanReference[] { adviceReference, pointcutReference };
        this.beanDefinitions = new BeanDefinition[] { advisorDefinition };
        this.description = buildDescription(adviceReference, pointcutReference);
    }
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) BeanReference(org.springframework.beans.factory.config.BeanReference)

Example 10 with BeanReference

use of org.springframework.beans.factory.config.BeanReference in project spring-framework by spring-projects.

the class ConfigBeanDefinitionParser method parseAspect.

private void parseAspect(Element aspectElement, ParserContext parserContext) {
    String aspectId = aspectElement.getAttribute(ID);
    String aspectName = aspectElement.getAttribute(REF);
    try {
        this.parseState.push(new AspectEntry(aspectId, aspectName));
        List<BeanDefinition> beanDefinitions = new ArrayList<>();
        List<BeanReference> beanReferences = new ArrayList<>();
        List<Element> declareParents = DomUtils.getChildElementsByTagName(aspectElement, DECLARE_PARENTS);
        for (int i = METHOD_INDEX; i < declareParents.size(); i++) {
            Element declareParentsElement = declareParents.get(i);
            beanDefinitions.add(parseDeclareParents(declareParentsElement, parserContext));
        }
        // We have to parse "advice" and all the advice kinds in one loop, to get the
        // ordering semantics right.
        NodeList nodeList = aspectElement.getChildNodes();
        boolean adviceFoundAlready = false;
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (isAdviceNode(node, parserContext)) {
                if (!adviceFoundAlready) {
                    adviceFoundAlready = true;
                    if (!StringUtils.hasText(aspectName)) {
                        parserContext.getReaderContext().error("<aspect> tag needs aspect bean reference via 'ref' attribute when declaring advices.", aspectElement, this.parseState.snapshot());
                        return;
                    }
                    beanReferences.add(new RuntimeBeanReference(aspectName));
                }
                AbstractBeanDefinition advisorDefinition = parseAdvice(aspectName, i, aspectElement, (Element) node, parserContext, beanDefinitions, beanReferences);
                beanDefinitions.add(advisorDefinition);
            }
        }
        AspectComponentDefinition aspectComponentDefinition = createAspectComponentDefinition(aspectElement, aspectId, beanDefinitions, beanReferences, parserContext);
        parserContext.pushContainingComponent(aspectComponentDefinition);
        List<Element> pointcuts = DomUtils.getChildElementsByTagName(aspectElement, POINTCUT);
        for (Element pointcutElement : pointcuts) {
            parsePointcut(pointcutElement, parserContext);
        }
        parserContext.popAndRegisterContainingComponent();
    } finally {
        this.parseState.pop();
    }
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanReference(org.springframework.beans.factory.config.BeanReference) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Aggregations

BeanReference (org.springframework.beans.factory.config.BeanReference)10 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)7 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)5 ManagedList (org.springframework.beans.factory.support.ManagedList)4 Element (org.w3c.dom.Element)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)2 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)2 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 PropertyValue (org.springframework.beans.PropertyValue)1 PropertyValues (org.springframework.beans.PropertyValues)1 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)1 ComponentDefinition (org.springframework.beans.factory.parsing.ComponentDefinition)1 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1