Search in sources :

Example 31 with RootBeanDefinition

use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-security by spring-projects.

the class LogoutBeanDefinitionParser method parse.

public BeanDefinition parse(Element element, ParserContext pc) {
    String logoutUrl = null;
    String successHandlerRef = null;
    String logoutSuccessUrl = null;
    String invalidateSession = null;
    String deleteCookies = null;
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(LogoutFilter.class);
    if (element != null) {
        Object source = pc.extractSource(element);
        builder.getRawBeanDefinition().setSource(source);
        logoutUrl = element.getAttribute(ATT_LOGOUT_URL);
        successHandlerRef = element.getAttribute(ATT_LOGOUT_HANDLER);
        WebConfigUtils.validateHttpRedirect(logoutUrl, pc, source);
        logoutSuccessUrl = element.getAttribute(ATT_LOGOUT_SUCCESS_URL);
        WebConfigUtils.validateHttpRedirect(logoutSuccessUrl, pc, source);
        invalidateSession = element.getAttribute(ATT_INVALIDATE_SESSION);
        deleteCookies = element.getAttribute(ATT_DELETE_COOKIES);
    }
    if (!StringUtils.hasText(logoutUrl)) {
        logoutUrl = DEF_LOGOUT_URL;
    }
    builder.addPropertyValue("logoutRequestMatcher", getLogoutRequestMatcher(logoutUrl));
    if (StringUtils.hasText(successHandlerRef)) {
        if (StringUtils.hasText(logoutSuccessUrl)) {
            pc.getReaderContext().error("Use " + ATT_LOGOUT_SUCCESS_URL + " or " + ATT_LOGOUT_HANDLER + ", but not both", pc.extractSource(element));
        }
        builder.addConstructorArgReference(successHandlerRef);
    } else {
        // Use the logout URL if no handler set
        if (!StringUtils.hasText(logoutSuccessUrl)) {
            logoutSuccessUrl = defaultLogoutUrl;
        }
        builder.addConstructorArgValue(logoutSuccessUrl);
    }
    BeanDefinition sclh = new RootBeanDefinition(SecurityContextLogoutHandler.class);
    sclh.getPropertyValues().addPropertyValue("invalidateHttpSession", !"false".equals(invalidateSession));
    logoutHandlers.add(sclh);
    if (rememberMeServices != null) {
        logoutHandlers.add(new RuntimeBeanReference(rememberMeServices));
    }
    if (StringUtils.hasText(deleteCookies)) {
        BeanDefinition cookieDeleter = new RootBeanDefinition(CookieClearingLogoutHandler.class);
        String[] names = StringUtils.tokenizeToStringArray(deleteCookies, ",");
        cookieDeleter.getConstructorArgumentValues().addGenericArgumentValue(names);
        logoutHandlers.add(cookieDeleter);
    }
    builder.addConstructorArgValue(logoutHandlers);
    return builder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 32 with RootBeanDefinition

use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-security by spring-projects.

the class PortMappingsBeanDefinitionParser method parse.

@SuppressWarnings("unchecked")
public BeanDefinition parse(Element element, ParserContext parserContext) {
    RootBeanDefinition portMapper = new RootBeanDefinition(PortMapperImpl.class);
    portMapper.setSource(parserContext.extractSource(element));
    if (element != null) {
        List<Element> mappingElts = DomUtils.getChildElementsByTagName(element, Elements.PORT_MAPPING);
        if (mappingElts.isEmpty()) {
            parserContext.getReaderContext().error("No port-mapping child elements specified", element);
        }
        Map mappings = new ManagedMap();
        for (Element elt : mappingElts) {
            String httpPort = elt.getAttribute(ATT_HTTP_PORT);
            String httpsPort = elt.getAttribute(ATT_HTTPS_PORT);
            if (!StringUtils.hasText(httpPort)) {
                parserContext.getReaderContext().error("No http port supplied in port mapping", elt);
            }
            if (!StringUtils.hasText(httpsPort)) {
                parserContext.getReaderContext().error("No https port supplied in port mapping", elt);
            }
            mappings.put(httpPort, httpsPort);
        }
        portMapper.getPropertyValues().addPropertyValue("portMappings", mappings);
    }
    return portMapper;
}
Also used : Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedMap(org.springframework.beans.factory.support.ManagedMap) Map(java.util.Map) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Example 33 with RootBeanDefinition

use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-security by spring-projects.

the class PasswordEncoderParser method parse.

private void parse(Element element, ParserContext parserContext) {
    String hash = element.getAttribute(ATT_HASH);
    boolean useBase64 = false;
    if (StringUtils.hasText(element.getAttribute(ATT_BASE_64))) {
        useBase64 = Boolean.valueOf(element.getAttribute(ATT_BASE_64)).booleanValue();
    }
    String ref = element.getAttribute(ATT_REF);
    if (StringUtils.hasText(ref)) {
        passwordEncoder = new RuntimeBeanReference(ref);
    } else {
        passwordEncoder = createPasswordEncoderBeanDefinition(hash, useBase64);
        ((RootBeanDefinition) passwordEncoder).setSource(parserContext.extractSource(element));
    }
    Element saltSourceElt = DomUtils.getChildElementByTagName(element, Elements.SALT_SOURCE);
    if (saltSourceElt != null) {
        if (OPT_HASH_BCRYPT.equals(hash)) {
            parserContext.getReaderContext().error(Elements.SALT_SOURCE + " isn't compatible with bcrypt", parserContext.extractSource(saltSourceElt));
        } else {
            saltSource = new SaltSourceBeanDefinitionParser().parse(saltSourceElt, parserContext);
        }
    }
}
Also used : BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 34 with RootBeanDefinition

use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-security by spring-projects.

the class UserServiceBeanDefinitionParser method doParse.

@SuppressWarnings("unchecked")
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    String userProperties = element.getAttribute(ATT_PROPERTIES);
    List<Element> userElts = DomUtils.getChildElementsByTagName(element, ELT_USER);
    if (StringUtils.hasText(userProperties)) {
        if (!CollectionUtils.isEmpty(userElts)) {
            throw new BeanDefinitionStoreException("Use of a properties file and user elements are mutually exclusive");
        }
        BeanDefinition bd = new RootBeanDefinition(PropertiesFactoryBean.class);
        bd.getPropertyValues().addPropertyValue("location", userProperties);
        builder.addConstructorArgValue(bd);
        return;
    }
    if (CollectionUtils.isEmpty(userElts)) {
        throw new BeanDefinitionStoreException("You must supply user definitions, either with <" + ELT_USER + "> child elements or a " + "properties file (using the '" + ATT_PROPERTIES + "' attribute)");
    }
    ManagedList<BeanDefinition> users = new ManagedList<BeanDefinition>();
    for (Object elt : userElts) {
        Element userElt = (Element) elt;
        String userName = userElt.getAttribute(ATT_NAME);
        String password = userElt.getAttribute(ATT_PASSWORD);
        if (!StringUtils.hasLength(password)) {
            password = generateRandomPassword();
        }
        boolean locked = "true".equals(userElt.getAttribute(ATT_LOCKED));
        boolean disabled = "true".equals(userElt.getAttribute(ATT_DISABLED));
        BeanDefinitionBuilder authorities = BeanDefinitionBuilder.rootBeanDefinition(AuthorityUtils.class);
        authorities.addConstructorArgValue(userElt.getAttribute(ATT_AUTHORITIES));
        authorities.setFactoryMethod("commaSeparatedStringToAuthorityList");
        BeanDefinitionBuilder user = BeanDefinitionBuilder.rootBeanDefinition(User.class);
        user.addConstructorArgValue(userName);
        user.addConstructorArgValue(password);
        user.addConstructorArgValue(!disabled);
        user.addConstructorArgValue(true);
        user.addConstructorArgValue(true);
        user.addConstructorArgValue(!locked);
        user.addConstructorArgValue(authorities.getBeanDefinition());
        users.add(user.getBeanDefinition());
    }
    builder.addConstructorArgValue(users);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 35 with RootBeanDefinition

use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-security by spring-projects.

the class AuthenticationConfigBuilder method createFormLoginFilter.

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);
        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);
    }
}
Also used : BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition)

Aggregations

RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)704 Test (org.junit.Test)522 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)311 TestBean (org.springframework.tests.sample.beans.TestBean)159 ITestBean (org.springframework.tests.sample.beans.ITestBean)143 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)133 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)85 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)80 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)77 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)77 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)47 Element (org.w3c.dom.Element)47 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)46 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)36 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)31 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)30 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)30 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)28 Properties (java.util.Properties)26 ManagedList (org.springframework.beans.factory.support.ManagedList)25