Search in sources :

Example 41 with BeanComponentDefinition

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

the class CsrfBeanDefinitionParser method parse.

@Override
public BeanDefinition parse(Element element, ParserContext pc) {
    boolean disabled = element != null && "true".equals(element.getAttribute("disabled"));
    if (disabled) {
        return null;
    }
    boolean webmvcPresent = ClassUtils.isPresent(DISPATCHER_SERVLET_CLASS_NAME, getClass().getClassLoader());
    if (webmvcPresent) {
        if (!pc.getRegistry().containsBeanDefinition(REQUEST_DATA_VALUE_PROCESSOR)) {
            RootBeanDefinition beanDefinition = new RootBeanDefinition(CsrfRequestDataValueProcessor.class);
            BeanComponentDefinition componentDefinition = new BeanComponentDefinition(beanDefinition, REQUEST_DATA_VALUE_PROCESSOR);
            pc.registerBeanComponent(componentDefinition);
        }
    }
    if (element != null) {
        this.csrfRepositoryRef = element.getAttribute(ATT_REPOSITORY);
        this.requestMatcherRef = element.getAttribute(ATT_MATCHER);
    }
    if (!StringUtils.hasText(this.csrfRepositoryRef)) {
        RootBeanDefinition csrfTokenRepository = new RootBeanDefinition(HttpSessionCsrfTokenRepository.class);
        BeanDefinitionBuilder lazyTokenRepository = BeanDefinitionBuilder.rootBeanDefinition(LazyCsrfTokenRepository.class);
        lazyTokenRepository.addConstructorArgValue(csrfTokenRepository);
        this.csrfRepositoryRef = pc.getReaderContext().generateBeanName(lazyTokenRepository.getBeanDefinition());
        pc.registerBeanComponent(new BeanComponentDefinition(lazyTokenRepository.getBeanDefinition(), this.csrfRepositoryRef));
    }
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(CsrfFilter.class);
    builder.addConstructorArgReference(this.csrfRepositoryRef);
    if (StringUtils.hasText(this.requestMatcherRef)) {
        builder.addPropertyReference("requireCsrfProtectionMatcher", this.requestMatcherRef);
    }
    this.csrfFilter = builder.getBeanDefinition();
    return this.csrfFilter;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition)

Example 42 with BeanComponentDefinition

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

the class FilterInvocationSecurityMetadataSourceParser method registerDefaultExpressionHandler.

static String registerDefaultExpressionHandler(ParserContext pc) {
    BeanDefinition expressionHandler = GrantedAuthorityDefaultsParserUtils.registerWithDefaultRolePrefix(pc, DefaultWebSecurityExpressionHandlerBeanFactory.class);
    String expressionHandlerRef = pc.getReaderContext().generateBeanName(expressionHandler);
    pc.registerBeanComponent(new BeanComponentDefinition(expressionHandler, expressionHandlerRef));
    return expressionHandlerRef;
}
Also used : BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 43 with BeanComponentDefinition

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

the class LdapServerBeanDefinitionParser method createEmbeddedServer.

/**
 * Will be called if no url attribute is supplied.
 *
 * Registers beans to create an embedded apache directory server.
 * @return the BeanDefinition for the ContextSource for the embedded server.
 *
 * @see ApacheDSContainer
 * @see UnboundIdContainer
 */
private RootBeanDefinition createEmbeddedServer(Element element, ParserContext parserContext) {
    Object source = parserContext.extractSource(element);
    String suffix = element.getAttribute(ATT_ROOT_SUFFIX);
    if (!StringUtils.hasText(suffix)) {
        suffix = OPT_DEFAULT_ROOT_SUFFIX;
    }
    BeanDefinitionBuilder contextSource = BeanDefinitionBuilder.rootBeanDefinition(CONTEXT_SOURCE_CLASS);
    contextSource.addConstructorArgValue(suffix);
    contextSource.addPropertyValue("userDn", "uid=admin,ou=system");
    contextSource.addPropertyValue("password", "secret");
    BeanDefinition embeddedLdapServerConfigBean = BeanDefinitionBuilder.rootBeanDefinition(EmbeddedLdapServerConfigBean.class).getBeanDefinition();
    String embeddedLdapServerConfigBeanName = parserContext.getReaderContext().generateBeanName(embeddedLdapServerConfigBean);
    parserContext.registerBeanComponent(new BeanComponentDefinition(embeddedLdapServerConfigBean, embeddedLdapServerConfigBeanName));
    contextSource.setFactoryMethodOnBean("createEmbeddedContextSource", embeddedLdapServerConfigBeanName);
    String mode = element.getAttribute("mode");
    RootBeanDefinition ldapContainer = getRootBeanDefinition(mode);
    ldapContainer.setSource(source);
    ldapContainer.getConstructorArgumentValues().addGenericArgumentValue(suffix);
    String ldifs = element.getAttribute(ATT_LDIF_FILE);
    if (!StringUtils.hasText(ldifs)) {
        ldifs = OPT_DEFAULT_LDIF_FILE;
    }
    ldapContainer.getConstructorArgumentValues().addGenericArgumentValue(ldifs);
    ldapContainer.getPropertyValues().addPropertyValue("port", getPort(element));
    if (parserContext.getRegistry().containsBeanDefinition(BeanIds.EMBEDDED_APACHE_DS) || parserContext.getRegistry().containsBeanDefinition(BeanIds.EMBEDDED_UNBOUNDID)) {
        parserContext.getReaderContext().error("Only one embedded server bean is allowed per application context", element);
    }
    String beanId = resolveBeanId(mode);
    if (beanId != null) {
        parserContext.getRegistry().registerBeanDefinition(beanId, ldapContainer);
    }
    return (RootBeanDefinition) contextSource.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 44 with BeanComponentDefinition

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

the class ClientRegistrationsBeanDefinitionParser method parse.

@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
    CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element));
    parserContext.pushContainingComponent(compositeDef);
    Map<String, Map<String, String>> providers = getProviders(element, parserContext);
    List<ClientRegistration> clientRegistrations = getClientRegistrations(element, parserContext, providers);
    BeanDefinition clientRegistrationRepositoryBean = BeanDefinitionBuilder.rootBeanDefinition(InMemoryClientRegistrationRepository.class).addConstructorArgValue(clientRegistrations).getBeanDefinition();
    String clientRegistrationRepositoryId = parserContext.getReaderContext().generateBeanName(clientRegistrationRepositoryBean);
    parserContext.registerBeanComponent(new BeanComponentDefinition(clientRegistrationRepositoryBean, clientRegistrationRepositoryId));
    parserContext.popAndRegisterContainingComponent();
    return null;
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) HashMap(java.util.HashMap) Map(java.util.Map)

Example 45 with BeanComponentDefinition

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

the class ResourcesBeanDefinitionParser method registerUrlProvider.

private void registerUrlProvider(ParserContext context, @Nullable Object source) {
    if (!context.getRegistry().containsBeanDefinition(RESOURCE_URL_PROVIDER)) {
        RootBeanDefinition urlProvider = new RootBeanDefinition(ResourceUrlProvider.class);
        urlProvider.setSource(source);
        urlProvider.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        context.getRegistry().registerBeanDefinition(RESOURCE_URL_PROVIDER, urlProvider);
        context.registerComponent(new BeanComponentDefinition(urlProvider, RESOURCE_URL_PROVIDER));
        RootBeanDefinition interceptor = new RootBeanDefinition(ResourceUrlProviderExposingInterceptor.class);
        interceptor.setSource(source);
        interceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, urlProvider);
        RootBeanDefinition mappedInterceptor = new RootBeanDefinition(MappedInterceptor.class);
        mappedInterceptor.setSource(source);
        mappedInterceptor.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, (Object) null);
        mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(1, interceptor);
        String mappedInterceptorName = context.getReaderContext().registerWithGeneratedName(mappedInterceptor);
        context.registerComponent(new BeanComponentDefinition(mappedInterceptor, mappedInterceptorName));
    }
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition)

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