Search in sources :

Example 56 with BeanComponentDefinition

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

the class MvcNamespaceUtils method registerCorsConfigurations.

/**
	 * Registers a {@code Map<String, CorsConfiguration>} (mapped {@code CorsConfiguration}s)
	 * under a well-known name unless already registered. The bean definition may be updated
	 * if a non-null CORS configuration is provided.
	 * @return a RuntimeBeanReference to this {@code Map<String, CorsConfiguration>} instance
	 */
public static RuntimeBeanReference registerCorsConfigurations(Map<String, CorsConfiguration> corsConfigurations, ParserContext parserContext, Object source) {
    if (!parserContext.getRegistry().containsBeanDefinition(CORS_CONFIGURATION_BEAN_NAME)) {
        RootBeanDefinition corsConfigurationsDef = new RootBeanDefinition(LinkedHashMap.class);
        corsConfigurationsDef.setSource(source);
        corsConfigurationsDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        if (corsConfigurations != null) {
            corsConfigurationsDef.getConstructorArgumentValues().addIndexedArgumentValue(0, corsConfigurations);
        }
        parserContext.getReaderContext().getRegistry().registerBeanDefinition(CORS_CONFIGURATION_BEAN_NAME, corsConfigurationsDef);
        parserContext.registerComponent(new BeanComponentDefinition(corsConfigurationsDef, CORS_CONFIGURATION_BEAN_NAME));
    } else if (corsConfigurations != null) {
        BeanDefinition corsConfigurationsDef = parserContext.getRegistry().getBeanDefinition(CORS_CONFIGURATION_BEAN_NAME);
        corsConfigurationsDef.getConstructorArgumentValues().addIndexedArgumentValue(0, corsConfigurations);
    }
    return new RuntimeBeanReference(CORS_CONFIGURATION_BEAN_NAME);
}
Also used : 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) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 57 with BeanComponentDefinition

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

the class ResourcesBeanDefinitionParser method registerResourceHandler.

private String registerResourceHandler(ParserContext parserContext, Element element, Object source) {
    String locationAttr = element.getAttribute("location");
    if (!StringUtils.hasText(locationAttr)) {
        parserContext.getReaderContext().error("The 'location' attribute is required.", parserContext.extractSource(element));
        return null;
    }
    ManagedList<String> locations = new ManagedList<>();
    locations.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(locationAttr)));
    RootBeanDefinition resourceHandlerDef = new RootBeanDefinition(ResourceHttpRequestHandler.class);
    resourceHandlerDef.setSource(source);
    resourceHandlerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    MutablePropertyValues values = resourceHandlerDef.getPropertyValues();
    values.add("locations", locations);
    String cacheSeconds = element.getAttribute("cache-period");
    if (StringUtils.hasText(cacheSeconds)) {
        values.add("cacheSeconds", cacheSeconds);
    }
    Element cacheControlElement = DomUtils.getChildElementByTagName(element, "cache-control");
    if (cacheControlElement != null) {
        CacheControl cacheControl = parseCacheControl(cacheControlElement);
        values.add("cacheControl", cacheControl);
    }
    Element resourceChainElement = DomUtils.getChildElementByTagName(element, "resource-chain");
    if (resourceChainElement != null) {
        parseResourceChain(resourceHandlerDef, parserContext, resourceChainElement, source);
    }
    Object manager = MvcNamespaceUtils.getContentNegotiationManager(parserContext);
    if (manager != null) {
        values.add("contentNegotiationManager", manager);
    }
    String beanName = parserContext.getReaderContext().generateBeanName(resourceHandlerDef);
    parserContext.getRegistry().registerBeanDefinition(beanName, resourceHandlerDef);
    parserContext.registerComponent(new BeanComponentDefinition(resourceHandlerDef, beanName));
    return beanName;
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) CacheControl(org.springframework.http.CacheControl)

Example 58 with BeanComponentDefinition

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

the class ClearCredentialsMethodInvokingFactoryBean 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);
    }
    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 : 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) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 59 with BeanComponentDefinition

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

the class ClearCredentialsMethodInvokingFactoryBean method registerFilterChainProxyIfNecessary.

static void registerFilterChainProxyIfNecessary(ParserContext pc, Object source) {
    if (pc.getRegistry().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));
    pc.getRegistry().registerAlias(BeanIds.FILTER_CHAIN_PROXY, BeanIds.SPRING_SECURITY_FILTER_CHAIN);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) 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) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 60 with BeanComponentDefinition

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

the class RememberMeBeanDefinitionParser method parse.

public BeanDefinition parse(Element element, ParserContext pc) {
    CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), pc.extractSource(element));
    pc.pushContainingComponent(compositeDef);
    String tokenRepository = element.getAttribute(ATT_TOKEN_REPOSITORY);
    String dataSource = element.getAttribute(ATT_DATA_SOURCE);
    String userServiceRef = element.getAttribute(ATT_USER_SERVICE_REF);
    String successHandlerRef = element.getAttribute(ATT_SUCCESS_HANDLER_REF);
    String rememberMeServicesRef = element.getAttribute(ATT_SERVICES_REF);
    String tokenValiditySeconds = element.getAttribute(ATT_TOKEN_VALIDITY);
    String useSecureCookie = element.getAttribute(ATT_SECURE_COOKIE);
    String remembermeParameter = element.getAttribute(ATT_FORM_REMEMBERME_PARAMETER);
    String remembermeCookie = element.getAttribute(ATT_REMEMBERME_COOKIE);
    Object source = pc.extractSource(element);
    RootBeanDefinition services = null;
    boolean dataSourceSet = StringUtils.hasText(dataSource);
    boolean tokenRepoSet = StringUtils.hasText(tokenRepository);
    boolean servicesRefSet = StringUtils.hasText(rememberMeServicesRef);
    boolean userServiceSet = StringUtils.hasText(userServiceRef);
    boolean useSecureCookieSet = StringUtils.hasText(useSecureCookie);
    boolean tokenValiditySet = StringUtils.hasText(tokenValiditySeconds);
    boolean remembermeParameterSet = StringUtils.hasText(remembermeParameter);
    boolean remembermeCookieSet = StringUtils.hasText(remembermeCookie);
    if (servicesRefSet && (dataSourceSet || tokenRepoSet || userServiceSet || tokenValiditySet || useSecureCookieSet || remembermeParameterSet || remembermeCookieSet)) {
        pc.getReaderContext().error(ATT_SERVICES_REF + " can't be used in combination with attributes " + ATT_TOKEN_REPOSITORY + "," + ATT_DATA_SOURCE + ", " + ATT_USER_SERVICE_REF + ", " + ATT_TOKEN_VALIDITY + ", " + ATT_SECURE_COOKIE + ", " + ATT_FORM_REMEMBERME_PARAMETER + " or " + ATT_REMEMBERME_COOKIE, source);
    }
    if (dataSourceSet && tokenRepoSet) {
        pc.getReaderContext().error("Specify " + ATT_TOKEN_REPOSITORY + " or " + ATT_DATA_SOURCE + " but not both", source);
    }
    boolean isPersistent = dataSourceSet | tokenRepoSet;
    if (isPersistent) {
        Object tokenRepo;
        services = new RootBeanDefinition(PersistentTokenBasedRememberMeServices.class);
        if (tokenRepoSet) {
            tokenRepo = new RuntimeBeanReference(tokenRepository);
        } else {
            tokenRepo = new RootBeanDefinition(JdbcTokenRepositoryImpl.class);
            ((BeanDefinition) tokenRepo).getPropertyValues().addPropertyValue("dataSource", new RuntimeBeanReference(dataSource));
        }
        services.getConstructorArgumentValues().addIndexedArgumentValue(2, tokenRepo);
    } else if (!servicesRefSet) {
        services = new RootBeanDefinition(TokenBasedRememberMeServices.class);
    }
    String servicesName;
    if (services != null) {
        RootBeanDefinition uds = new RootBeanDefinition();
        uds.setFactoryBeanName(BeanIds.USER_DETAILS_SERVICE_FACTORY);
        uds.setFactoryMethodName("cachingUserDetailsService");
        uds.getConstructorArgumentValues().addGenericArgumentValue(userServiceRef);
        services.getConstructorArgumentValues().addGenericArgumentValue(key);
        services.getConstructorArgumentValues().addGenericArgumentValue(uds);
        if (useSecureCookieSet) {
            services.getPropertyValues().addPropertyValue("useSecureCookie", Boolean.valueOf(useSecureCookie));
        }
        if (tokenValiditySet) {
            boolean isTokenValidityNegative = tokenValiditySeconds.startsWith("-");
            if (isTokenValidityNegative && isPersistent) {
                pc.getReaderContext().error(ATT_TOKEN_VALIDITY + " cannot be negative if using" + " a persistent remember-me token repository", source);
            }
            services.getPropertyValues().addPropertyValue("tokenValiditySeconds", tokenValiditySeconds);
        }
        if (remembermeParameterSet) {
            services.getPropertyValues().addPropertyValue("parameter", remembermeParameter);
        }
        if (remembermeCookieSet) {
            services.getPropertyValues().addPropertyValue("cookieName", remembermeCookie);
        }
        services.setSource(source);
        servicesName = pc.getReaderContext().generateBeanName(services);
        pc.registerBeanComponent(new BeanComponentDefinition(services, servicesName));
    } else {
        servicesName = rememberMeServicesRef;
    }
    if (StringUtils.hasText(element.getAttribute(ATT_SERVICES_ALIAS))) {
        pc.getRegistry().registerAlias(servicesName, element.getAttribute(ATT_SERVICES_ALIAS));
    }
    this.rememberMeServicesId = servicesName;
    BeanDefinitionBuilder filter = BeanDefinitionBuilder.rootBeanDefinition(RememberMeAuthenticationFilter.class);
    filter.getRawBeanDefinition().setSource(source);
    if (StringUtils.hasText(successHandlerRef)) {
        filter.addPropertyReference("authenticationSuccessHandler", successHandlerRef);
    }
    filter.addConstructorArgValue(authenticationManager);
    filter.addConstructorArgReference(servicesName);
    pc.popAndRegisterContainingComponent();
    return filter.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) PersistentTokenBasedRememberMeServices(org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices) JdbcTokenRepositoryImpl(org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Aggregations

BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)82 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)53 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)33 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)28 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)24 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)17 Element (org.w3c.dom.Element)16 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)11 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)11 ManagedList (org.springframework.beans.factory.support.ManagedList)10 Test (org.junit.Test)7 ComponentDefinition (org.springframework.beans.factory.parsing.ComponentDefinition)7 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)6 ManagedMap (org.springframework.beans.factory.support.ManagedMap)4 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)3 BeanComponentDefinitionBuilder (org.springframework.data.config.BeanComponentDefinitionBuilder)3 Method (java.lang.reflect.Method)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 PropertyValue (org.springframework.beans.PropertyValue)2 BeanReference (org.springframework.beans.factory.config.BeanReference)2