Search in sources :

Example 36 with BeanComponentDefinition

use of org.springframework.beans.factory.parsing.BeanComponentDefinition in project spring-security by spring-projects.

the class HttpSecurityBeanDefinitionParser method createSecurityFilterChainBean.

private BeanReference createSecurityFilterChainBean(Element element, ParserContext pc, List<?> filterChain) {
    BeanMetadataElement filterChainMatcher;
    String requestMatcherRef = element.getAttribute(ATT_REQUEST_MATCHER_REF);
    String filterChainPattern = element.getAttribute(ATT_PATH_PATTERN);
    if (StringUtils.hasText(requestMatcherRef)) {
        if (StringUtils.hasText(filterChainPattern)) {
            pc.getReaderContext().error("You can't define a pattern and a request-matcher-ref for the " + "same filter chain", pc.extractSource(element));
        }
        filterChainMatcher = new RuntimeBeanReference(requestMatcherRef);
    } else if (StringUtils.hasText(filterChainPattern)) {
        filterChainMatcher = MatcherType.fromElement(element).createMatcher(pc, filterChainPattern, null);
    } else {
        filterChainMatcher = new RootBeanDefinition(AnyRequestMatcher.class);
    }
    BeanDefinitionBuilder filterChainBldr = BeanDefinitionBuilder.rootBeanDefinition(DefaultSecurityFilterChain.class);
    filterChainBldr.addConstructorArgValue(filterChainMatcher);
    filterChainBldr.addConstructorArgValue(filterChain);
    BeanDefinition filterChainBean = filterChainBldr.getBeanDefinition();
    String id = element.getAttribute("name");
    if (!StringUtils.hasText(id)) {
        id = element.getAttribute("id");
        if (!StringUtils.hasText(id)) {
            id = pc.getReaderContext().generateBeanName(filterChainBean);
        }
    }
    pc.registerBeanComponent(new BeanComponentDefinition(filterChainBean, id));
    return new RuntimeBeanReference(id);
}
Also used : 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) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 37 with BeanComponentDefinition

use of org.springframework.beans.factory.parsing.BeanComponentDefinition in project spring-security by spring-projects.

the class HttpSecurityBeanDefinitionParser method createPortMapper.

private BeanReference createPortMapper(Element elt, ParserContext pc) {
    // Register the portMapper. A default will always be created, even if no element
    // exists.
    BeanDefinition portMapper = new PortMappingsBeanDefinitionParser().parse(DomUtils.getChildElementByTagName(elt, Elements.PORT_MAPPINGS), pc);
    String portMapperName = pc.getReaderContext().generateBeanName(portMapper);
    pc.registerBeanComponent(new BeanComponentDefinition(portMapper, portMapperName));
    return new RuntimeBeanReference(portMapperName);
}
Also used : BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 38 with BeanComponentDefinition

use of org.springframework.beans.factory.parsing.BeanComponentDefinition in project spring-security by spring-projects.

the class HttpSecurityBeanDefinitionParser method createAuthenticationManager.

/**
 * Creates the internal AuthenticationManager bean which uses either the externally
 * registered (global) one as a parent or the bean specified by
 * "authentication-manager-ref".
 *
 * All the providers registered by this &lt;http&gt; block will be registered with the
 * internal authentication manager.
 */
private BeanReference createAuthenticationManager(Element element, ParserContext pc, ManagedList<BeanReference> authenticationProviders) {
    String parentMgrRef = element.getAttribute(ATT_AUTHENTICATION_MANAGER_REF);
    BeanDefinitionBuilder authManager = BeanDefinitionBuilder.rootBeanDefinition(ProviderManager.class);
    authManager.addConstructorArgValue(authenticationProviders);
    if (StringUtils.hasText(parentMgrRef)) {
        RuntimeBeanReference parentAuthManager = new RuntimeBeanReference(parentMgrRef);
        authManager.addConstructorArgValue(parentAuthManager);
        RootBeanDefinition clearCredentials = new RootBeanDefinition(ClearCredentialsMethodInvokingFactoryBean.class);
        clearCredentials.getPropertyValues().addPropertyValue("targetObject", parentAuthManager);
        clearCredentials.getPropertyValues().addPropertyValue("targetMethod", "isEraseCredentialsAfterAuthentication");
        authManager.addPropertyValue("eraseCredentialsAfterAuthentication", clearCredentials);
    } else {
        RootBeanDefinition amfb = new RootBeanDefinition(AuthenticationManagerFactoryBean.class);
        amfb.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        String amfbId = pc.getReaderContext().generateBeanName(amfb);
        pc.registerBeanComponent(new BeanComponentDefinition(amfb, amfbId));
        RootBeanDefinition clearCredentials = new RootBeanDefinition(MethodInvokingFactoryBean.class);
        clearCredentials.getPropertyValues().addPropertyValue("targetObject", new RuntimeBeanReference(amfbId));
        clearCredentials.getPropertyValues().addPropertyValue("targetMethod", "isEraseCredentialsAfterAuthentication");
        authManager.addConstructorArgValue(new RuntimeBeanReference(amfbId));
        authManager.addPropertyValue("eraseCredentialsAfterAuthentication", clearCredentials);
    }
    // gh-6009
    authManager.addPropertyValue("authenticationEventPublisher", new RootBeanDefinition(DefaultAuthenticationEventPublisher.class));
    authManager.getRawBeanDefinition().setSource(pc.extractSource(element));
    BeanDefinition authMgrBean = authManager.getBeanDefinition();
    String id = pc.getReaderContext().generateBeanName(authMgrBean);
    pc.registerBeanComponent(new BeanComponentDefinition(authMgrBean, id));
    return new RuntimeBeanReference(id);
}
Also used : DefaultAuthenticationEventPublisher(org.springframework.security.authentication.DefaultAuthenticationEventPublisher) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 39 with BeanComponentDefinition

use of org.springframework.beans.factory.parsing.BeanComponentDefinition in project spring-security by spring-projects.

the class HttpSecurityBeanDefinitionParser method registerFilterChainProxyIfNecessary.

static void registerFilterChainProxyIfNecessary(ParserContext pc, Object source) {
    BeanDefinitionRegistry registry = pc.getRegistry();
    if (registry.containsBeanDefinition(BeanIds.FILTER_CHAIN_PROXY)) {
        return;
    }
    // Not already registered, so register the list of filter chains and the
    // FilterChainProxy
    BeanDefinition listFactoryBean = new RootBeanDefinition(ListFactoryBean.class);
    listFactoryBean.getPropertyValues().add("sourceList", new ManagedList());
    pc.registerBeanComponent(new BeanComponentDefinition(listFactoryBean, BeanIds.FILTER_CHAINS));
    BeanDefinitionBuilder fcpBldr = BeanDefinitionBuilder.rootBeanDefinition(FilterChainProxy.class);
    fcpBldr.getRawBeanDefinition().setSource(source);
    fcpBldr.addConstructorArgReference(BeanIds.FILTER_CHAINS);
    fcpBldr.addPropertyValue("filterChainValidator", new RootBeanDefinition(DefaultFilterChainValidator.class));
    BeanDefinition fcpBean = fcpBldr.getBeanDefinition();
    pc.registerBeanComponent(new BeanComponentDefinition(fcpBean, BeanIds.FILTER_CHAIN_PROXY));
    registry.registerAlias(BeanIds.FILTER_CHAIN_PROXY, BeanIds.SPRING_SECURITY_FILTER_CHAIN);
    BeanDefinitionBuilder requestRejected = BeanDefinitionBuilder.rootBeanDefinition(RequestRejectedHandlerPostProcessor.class);
    requestRejected.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    requestRejected.addConstructorArgValue("requestRejectedHandler");
    requestRejected.addConstructorArgValue(BeanIds.FILTER_CHAIN_PROXY);
    requestRejected.addConstructorArgValue("requestRejectedHandler");
    AbstractBeanDefinition requestRejectedBean = requestRejected.getBeanDefinition();
    String requestRejectedPostProcessorName = pc.getReaderContext().generateBeanName(requestRejectedBean);
    registry.registerBeanDefinition(requestRejectedPostProcessorName, requestRejectedBean);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 40 with BeanComponentDefinition

use of org.springframework.beans.factory.parsing.BeanComponentDefinition in project spring-security by spring-projects.

the class GlobalMethodSecurityBeanDefinitionParser method registerMethodSecurityInterceptor.

private BeanReference registerMethodSecurityInterceptor(ParserContext pc, String authMgrRef, String accessManagerId, String runAsManagerId, BeanReference metadataSource, List<BeanMetadataElement> afterInvocationProviders, Object source, boolean useAspectJ) {
    BeanDefinitionBuilder bldr = BeanDefinitionBuilder.rootBeanDefinition(useAspectJ ? AspectJMethodSecurityInterceptor.class : MethodSecurityInterceptor.class);
    bldr.getRawBeanDefinition().setSource(source);
    bldr.addPropertyReference("accessDecisionManager", accessManagerId);
    RootBeanDefinition authMgr = new RootBeanDefinition(AuthenticationManagerDelegator.class);
    authMgr.getConstructorArgumentValues().addGenericArgumentValue(authMgrRef);
    bldr.addPropertyValue("authenticationManager", authMgr);
    bldr.addPropertyValue("securityMetadataSource", metadataSource);
    if (StringUtils.hasText(runAsManagerId)) {
        bldr.addPropertyReference("runAsManager", runAsManagerId);
    }
    if (!afterInvocationProviders.isEmpty()) {
        BeanDefinition afterInvocationManager;
        afterInvocationManager = new RootBeanDefinition(AfterInvocationProviderManager.class);
        afterInvocationManager.getPropertyValues().addPropertyValue("providers", afterInvocationProviders);
        bldr.addPropertyValue("afterInvocationManager", afterInvocationManager);
    }
    BeanDefinition bean = bldr.getBeanDefinition();
    String id = pc.getReaderContext().generateBeanName(bean);
    pc.registerBeanComponent(new BeanComponentDefinition(bean, id));
    return new RuntimeBeanReference(id);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) AfterInvocationProviderManager(org.springframework.security.access.intercept.AfterInvocationProviderManager) AspectJMethodSecurityInterceptor(org.springframework.security.access.intercept.aspectj.AspectJMethodSecurityInterceptor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AspectJMethodSecurityInterceptor(org.springframework.security.access.intercept.aspectj.AspectJMethodSecurityInterceptor) MethodSecurityInterceptor(org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Aggregations

BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)133 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)75 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)52 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)47 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)47 Element (org.w3c.dom.Element)29 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)22 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)22 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)20 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)16 ManagedList (org.springframework.beans.factory.support.ManagedList)14 Nullable (org.springframework.lang.Nullable)10 ComponentDefinition (org.springframework.beans.factory.parsing.ComponentDefinition)9 Test (org.junit.jupiter.api.Test)6 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)5 NodeList (org.w3c.dom.NodeList)5 Test (org.junit.Test)4 ManagedMap (org.springframework.beans.factory.support.ManagedMap)4 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)3 PropertyValue (org.springframework.beans.PropertyValue)3