Example usage for org.springframework.beans.factory.parsing BeanComponentDefinition BeanComponentDefinition

List of usage examples for org.springframework.beans.factory.parsing BeanComponentDefinition BeanComponentDefinition

Introduction

In this page you can find the example usage for org.springframework.beans.factory.parsing BeanComponentDefinition BeanComponentDefinition.

Prototype

public BeanComponentDefinition(BeanDefinition beanDefinition, String beanName) 

Source Link

Document

Create a new BeanComponentDefinition for the given bean.

Usage

From source file:org.springframework.security.config.http.AuthenticationConfigBuilder.java

private void createRememberMeProvider(String key) {
    RootBeanDefinition provider = new RootBeanDefinition(RememberMeAuthenticationProvider.class);
    provider.setSource(rememberMeFilter.getSource());

    provider.getConstructorArgumentValues().addGenericArgumentValue(key);

    String id = pc.getReaderContext().generateBeanName(provider);
    pc.registerBeanComponent(new BeanComponentDefinition(provider, id));

    rememberMeProviderRef = new RuntimeBeanReference(id);
}

From source file:org.springframework.security.config.http.AuthenticationConfigBuilder.java

void createFormLoginFilter(BeanReference sessionStrategy, BeanReference authManager) {

    Element formLoginElt = DomUtils.getChildElementByTagName(httpElt, Elements.FORM_LOGIN);
    RootBeanDefinition formFilter = null;

    if (formLoginElt != null || autoConfig) {
        FormLoginBeanDefinitionParser parser = new FormLoginBeanDefinitionParser("/login", "POST",
                AUTHENTICATION_PROCESSING_FILTER_CLASS, requestCache, sessionStrategy, allowSessionCreation,
                portMapper, portResolver);

        parser.parse(formLoginElt, pc);//from   w  w  w.  j  a  v  a2  s. c  o  m
        formFilter = parser.getFilterBean();
        formEntryPoint = parser.getEntryPointBean();
        loginProcessingUrl = parser.getLoginProcessingUrl();
        formLoginPage = parser.getLoginPage();
    }

    if (formFilter != null) {
        formFilter.getPropertyValues().addPropertyValue("allowSessionCreation", allowSessionCreation);
        formFilter.getPropertyValues().addPropertyValue("authenticationManager", authManager);

        // Id is required by login page filter
        formFilterId = pc.getReaderContext().generateBeanName(formFilter);
        pc.registerBeanComponent(new BeanComponentDefinition(formFilter, formFilterId));
        injectRememberMeServicesRef(formFilter, rememberMeServicesId);
    }
}

From source file:org.springframework.security.config.http.AuthenticationConfigBuilder.java

void createOpenIDLoginFilter(BeanReference sessionStrategy, BeanReference authManager) {
    Element openIDLoginElt = DomUtils.getChildElementByTagName(httpElt, Elements.OPENID_LOGIN);
    RootBeanDefinition openIDFilter = null;

    if (openIDLoginElt != null) {
        FormLoginBeanDefinitionParser parser = new FormLoginBeanDefinitionParser("/login/openid", null,
                OPEN_ID_AUTHENTICATION_PROCESSING_FILTER_CLASS, requestCache, sessionStrategy,
                allowSessionCreation, portMapper, portResolver);

        parser.parse(openIDLoginElt, pc);
        openIDFilter = parser.getFilterBean();
        openIDEntryPoint = parser.getEntryPointBean();
        openidLoginProcessingUrl = parser.getLoginProcessingUrl();
        openIDLoginPage = parser.getLoginPage();

        List<Element> attrExElts = DomUtils.getChildElementsByTagName(openIDLoginElt,
                Elements.OPENID_ATTRIBUTE_EXCHANGE);

        if (!attrExElts.isEmpty()) {
            // Set up the consumer with the required attribute list
            BeanDefinitionBuilder consumerBldr = BeanDefinitionBuilder
                    .rootBeanDefinition(OPEN_ID_CONSUMER_CLASS);
            BeanDefinitionBuilder axFactory = BeanDefinitionBuilder
                    .rootBeanDefinition(OPEN_ID_ATTRIBUTE_FACTORY_CLASS);
            ManagedMap<String, ManagedList<BeanDefinition>> axMap = new ManagedMap<String, ManagedList<BeanDefinition>>();

            for (Element attrExElt : attrExElts) {
                String identifierMatch = attrExElt.getAttribute("identifier-match");

                if (!StringUtils.hasText(identifierMatch)) {
                    if (attrExElts.size() > 1) {
                        pc.getReaderContext()
                                .error("You must supply an identifier-match attribute if using more"
                                        + " than one " + Elements.OPENID_ATTRIBUTE_EXCHANGE + " element",
                                        attrExElt);
                    }/*from  ww  w . j  a va2  s .  com*/
                    // Match anything
                    identifierMatch = ".*";
                }

                axMap.put(identifierMatch, parseOpenIDAttributes(attrExElt));
            }
            axFactory.addConstructorArgValue(axMap);

            consumerBldr.addConstructorArgValue(axFactory.getBeanDefinition());
            openIDFilter.getPropertyValues().addPropertyValue("consumer", consumerBldr.getBeanDefinition());
        }
    }

    if (openIDFilter != null) {
        openIDFilter.getPropertyValues().addPropertyValue("allowSessionCreation", allowSessionCreation);
        openIDFilter.getPropertyValues().addPropertyValue("authenticationManager", authManager);
        // Required by login page filter
        openIDFilterId = pc.getReaderContext().generateBeanName(openIDFilter);
        pc.registerBeanComponent(new BeanComponentDefinition(openIDFilter, openIDFilterId));
        injectRememberMeServicesRef(openIDFilter, rememberMeServicesId);

        createOpenIDProvider();
    }
}

From source file:org.springframework.security.config.http.AuthenticationConfigBuilder.java

void createBasicFilter(BeanReference authManager) {
    Element basicAuthElt = DomUtils.getChildElementByTagName(httpElt, Elements.BASIC_AUTH);

    if (basicAuthElt == null && !autoConfig) {
        // No basic auth, do nothing
        return;/* ww w .j  av  a  2 s  .  com*/
    }

    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();
}

From source file:org.springframework.security.config.http.AuthenticationConfigBuilder.java

void createAnonymousFilter() {
    Element anonymousElt = DomUtils.getChildElementByTagName(httpElt, Elements.ANONYMOUS);

    if (anonymousElt != null && "false".equals(anonymousElt.getAttribute("enabled"))) {
        return;/*  w  w  w.  j a  v  a  2  s. com*/
    }

    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);

}

From source file:org.springframework.security.config.http.AuthenticationConfigBuilder.java

private void createUserDetailsServiceFactory() {
    if (pc.getRegistry().containsBeanDefinition(BeanIds.USER_DETAILS_SERVICE_FACTORY)) {
        // Multiple <http> case
        return;/*from ww w  .j a  va  2  s. co m*/
    }
    RootBeanDefinition bean = new RootBeanDefinition(UserDetailsServiceFactoryBean.class);
    bean.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    pc.registerBeanComponent(new BeanComponentDefinition(bean, BeanIds.USER_DETAILS_SERVICE_FACTORY));
}

From source file:org.springframework.security.config.http.FilterInvocationSecurityMetadataSourceParser.java

public BeanDefinition parse(Element element, ParserContext parserContext) {
    List<Element> interceptUrls = DomUtils.getChildElementsByTagName(element, Elements.INTERCEPT_URL);

    // Check for attributes that aren't allowed in this context
    for (Element elt : interceptUrls) {
        if (StringUtils.hasLength(elt.getAttribute(HttpSecurityBeanDefinitionParser.ATT_REQUIRES_CHANNEL))) {
            parserContext.getReaderContext().error("The attribute '"
                    + HttpSecurityBeanDefinitionParser.ATT_REQUIRES_CHANNEL + "' isn't allowed here.", elt);
        }//from w w w .j  a  va2 s  . com

        if (StringUtils.hasLength(elt.getAttribute(HttpSecurityBeanDefinitionParser.ATT_FILTERS))) {
            parserContext.getReaderContext().error(
                    "The attribute '" + HttpSecurityBeanDefinitionParser.ATT_FILTERS + "' isn't allowed here.",
                    elt);
        }

        if (StringUtils.hasLength(elt.getAttribute(ATT_SERVLET_PATH))) {
            parserContext.getReaderContext()
                    .error("The attribute '" + ATT_SERVLET_PATH + "' isn't allowed here.", elt);
        }
    }

    BeanDefinition mds = createSecurityMetadataSource(interceptUrls, false, element, parserContext);

    String id = element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE);

    if (StringUtils.hasText(id)) {
        parserContext.registerComponent(new BeanComponentDefinition(mds, id));
        parserContext.getRegistry().registerBeanDefinition(id, mds);
    }

    return mds;
}

From source file:org.springframework.security.config.http.FilterInvocationSecurityMetadataSourceParser.java

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;
}

From source file:org.springframework.security.config.http.HttpSecurityBeanDefinitionParser.java

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));
        }//ww  w  .j  a va 2 s  .  c om
        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);
}

From source file:org.springframework.security.config.http.HttpSecurityBeanDefinitionParser.java

private BeanReference createPortMapper(Element elt, ParserContext pc) {
    // Register the portMapper. A default will always be created, even if no element
    // exists.//from w ww. j  a  va  2s. co  m
    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);
}