From 5f98ee68179cb532dffbb6ef5b63aef2b90a46ba Mon Sep 17 00:00:00 2001 From: Ben Alex Date: Tue, 4 Dec 2007 05:54:58 +0000 Subject: [PATCH] element and JdbcUserDetailsManager support. --- .../security/config/BeanIds.java | 13 ++++ .../security/config/Elements.java | 15 ++++ .../RepositoryBeanDefinitionParser.java | 76 +++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 core/src/main/java/org/springframework/security/config/BeanIds.java create mode 100644 core/src/main/java/org/springframework/security/config/Elements.java create mode 100644 core/src/main/java/org/springframework/security/config/RepositoryBeanDefinitionParser.java diff --git a/core/src/main/java/org/springframework/security/config/BeanIds.java b/core/src/main/java/org/springframework/security/config/BeanIds.java new file mode 100644 index 0000000000..2c46c1ee5d --- /dev/null +++ b/core/src/main/java/org/springframework/security/config/BeanIds.java @@ -0,0 +1,13 @@ +package org.springframework.security.config; + +/** + * Contains all the default Bean IDs created by the namespace support in Spring Security 2. + * + * @author Ben Alex + * @version $Id$ + */ +class BeanIds { + + static final String JDBC_USER_DETAILS_MANAGER = "_jdbcUserDetailsManager"; + +} diff --git a/core/src/main/java/org/springframework/security/config/Elements.java b/core/src/main/java/org/springframework/security/config/Elements.java new file mode 100644 index 0000000000..1251aa0ff9 --- /dev/null +++ b/core/src/main/java/org/springframework/security/config/Elements.java @@ -0,0 +1,15 @@ +package org.springframework.security.config; + +/** + * Contains all the element names used by Spring Security 2 namespace support. + * + * @author Ben Alex + * @version $Id$ + */ +class Elements { + + static final String ELT_USER_SERVICE = "user-service"; + static final String ELT_JDBC_USER_SERVICE = "jdbc-user-service"; + static final String ELT_CUSTOM_USER_SERVICE = "custom-user-service"; + +} diff --git a/core/src/main/java/org/springframework/security/config/RepositoryBeanDefinitionParser.java b/core/src/main/java/org/springframework/security/config/RepositoryBeanDefinitionParser.java new file mode 100644 index 0000000000..1b7f797840 --- /dev/null +++ b/core/src/main/java/org/springframework/security/config/RepositoryBeanDefinitionParser.java @@ -0,0 +1,76 @@ +package org.springframework.security.config; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.beans.factory.xml.BeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.security.providers.dao.DaoAuthenticationProvider; +import org.springframework.security.userdetails.jdbc.JdbcUserDetailsManager; +import org.springframework.util.StringUtils; +import org.springframework.util.xml.DomUtils; +import org.w3c.dom.Element; + +/** + * Processes the top-level "repository" element. + * + *

A "repository" element is used to indicate a UserDetailsService or equivalent. + * + * @author Ben Alex + * @version $Id$ + */ +class RepositoryBeanDefinitionParser implements BeanDefinitionParser { + + private static final String ATT_CREATE_PROVIDER = "createProvider"; + private static final String DEF_CREATE_PROVIDER = "true"; + private static final String ATT_DATA_SOURCE = "dataSource"; + private static final String ATT_ID = "id"; + + public BeanDefinition parse(Element element, ParserContext parserContext) { + boolean createProvider = true; + String createProviderAtt = element.getAttribute(ATT_CREATE_PROVIDER); + if (StringUtils.hasText(createProviderAtt) && "false".equals(createProviderAtt)) { + createProvider = false; + } + + if (createProvider) { + ConfigUtils.registerProviderManagerIfNecessary(parserContext); + } + + Element userServiceElt = DomUtils.getChildElementByTagName(element, Elements.ELT_USER_SERVICE); + Element jdbcUserServiceElt = DomUtils.getChildElementByTagName(element, Elements.ELT_JDBC_USER_SERVICE); + Element customUserServiceElt = DomUtils.getChildElementByTagName(element, Elements.ELT_CUSTOM_USER_SERVICE); + + if (userServiceElt != null) { + BeanDefinition userDetailsService = new UserServiceBeanDefinitionParser().parse(userServiceElt, parserContext); + createDaoAuthenticationProviderIfRequired(createProvider, userDetailsService, parserContext); + } + + if (jdbcUserServiceElt != null) { + // TODO: Set authenticationManager property + // TODO: Have some sensible fallback if dataSource not specified, eg autowire + BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(JdbcUserDetailsManager.class); + String dataSource = jdbcUserServiceElt.getAttribute(ATT_DATA_SOURCE); + // An explicit dataSource was specified, so use it + builder.addPropertyReference("dataSource", dataSource); + parserContext.getRegistry().registerBeanDefinition(BeanIds.JDBC_USER_DETAILS_MANAGER, builder.getBeanDefinition()); + createDaoAuthenticationProviderIfRequired(createProvider, builder.getBeanDefinition(), parserContext); + } + + if (customUserServiceElt != null) { + String id = customUserServiceElt.getAttribute(ATT_ID); + BeanDefinition userDetailsService = parserContext.getRegistry().getBeanDefinition(id); + createDaoAuthenticationProviderIfRequired(createProvider, userDetailsService, parserContext); + } + + return null; + } + + private void createDaoAuthenticationProviderIfRequired(boolean createProvider, BeanDefinition userDetailsService, ParserContext parserContext) { + if (createProvider) { + RootBeanDefinition authProvider = new RootBeanDefinition(DaoAuthenticationProvider.class); + authProvider.getPropertyValues().addPropertyValue("userDetailsService", userDetailsService); + ConfigUtils.getRegisteredProviders(parserContext).add(authProvider); + } + } +}