Moved responsability for creating AuthenticationManager into AuthenticationProviderBeanDefinitionParser. Tidied up SecrityNamespaceHandler.

This commit is contained in:
Luke Taylor 2007-10-25 14:21:45 +00:00
parent 3927ba9ed0
commit 03e285c31d
4 changed files with 44 additions and 78 deletions

View File

@ -1,47 +1,58 @@
package org.springframework.security.config;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.ManagedList;
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.ProviderManager;
import org.springframework.security.providers.dao.DaoAuthenticationProvider;
import org.springframework.security.ui.logout.LogoutFilter;
import org.springframework.util.xml.DomUtils;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* @author luke
* @author Luke Taylor
* @version $Id$
*/
public class AuthenticationProviderBeanDefinitionParser extends AbstractBeanDefinitionParser {
private static final String DEFAULT_PROVIDER_BEAN_ID = "_authenticationProvider";
class AuthenticationProviderBeanDefinitionParser implements BeanDefinitionParser {
public static final String DEFAULT_AUTH_MANAGER_ID = "_authenticationManager";
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
RootBeanDefinition authProvider = new RootBeanDefinition(DaoAuthenticationProvider.class);
private BeanDefinition registerProviderManagerIfNecessary(ParserContext parserContext) {
if(parserContext.getRegistry().containsBeanDefinition(DEFAULT_AUTH_MANAGER_ID)) {
return parserContext.getRegistry().getBeanDefinition(DEFAULT_AUTH_MANAGER_ID);
}
BeanDefinition authManager = new RootBeanDefinition(ProviderManager.class);
authManager.getPropertyValues().addPropertyValue("providers", new ManagedList());
parserContext.getRegistry().registerBeanDefinition(DEFAULT_AUTH_MANAGER_ID, authManager);
return authManager;
}
private ManagedList getRegisteredProviders(ParserContext parserContext) {
BeanDefinition authManager = registerProviderManagerIfNecessary(parserContext);
return (ManagedList) authManager.getPropertyValues().getPropertyValue("providers").getValue();
}
public BeanDefinition parse(Element element, ParserContext parserContext) {
registerProviderManagerIfNecessary(parserContext);
RootBeanDefinition authProvider;
// TODO: Proper implementation
Element userServiceElt = DomUtils.getChildElementByTagName(element, "user-service");
BeanDefinition userDetailsService = new UserServiceBeanDefinitionParser().parse(userServiceElt, parserContext);
authProvider.getPropertyValues().addPropertyValue("userDetailsService", userDetailsService);
return authProvider;
}
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) throws BeanDefinitionStoreException {
String id = super.resolveId(element, definition, parserContext);
if (StringUtils.hasText(id)) {
return id;
if (userServiceElt != null) {
authProvider = new RootBeanDefinition(DaoAuthenticationProvider.class);
BeanDefinition userDetailsService = new UserServiceBeanDefinitionParser().parse(userServiceElt, parserContext);
authProvider.getPropertyValues().addPropertyValue("userDetailsService", userDetailsService);
} else {
throw new IllegalArgumentException("Only support user-service provider at the moment.");
}
// TODO: Check for duplicate using default id here.
return DEFAULT_PROVIDER_BEAN_ID;
}
getRegisteredProviders(parserContext).add(authProvider);
return null;
}
}

View File

@ -22,19 +22,14 @@ import java.util.ArrayList;
import java.util.Arrays;
/**
* @author luke
* @author Luke Taylor
* @version $Id$
*/
public class AutoConfigBeanDefinitionParser implements BeanDefinitionParser {
public static final String AUTH_MANAGER_ID = "_authenticationManager";
public static final String DEFAULT_ACCESS_MANAGER_ID = "_accessManager";
public BeanDefinition parse(Element element, ParserContext parserContext) {
RootBeanDefinition authenticationManager = new RootBeanDefinition(ProviderManager.class);
BeanDefinitionRegistry registry = parserContext.getRegistry();
registry.registerBeanDefinition(AUTH_MANAGER_ID, authenticationManager);
registry.registerBeanDefinition("_autoconfigPP", new RootBeanDefinition(AutoConfigPostProcessor.class));
RootBeanDefinition accessManager = new RootBeanDefinition(AffirmativeBased.class);
@ -44,22 +39,4 @@ public class AutoConfigBeanDefinitionParser implements BeanDefinitionParser {
registry.registerBeanDefinition(DEFAULT_ACCESS_MANAGER_ID, accessManager);
return null;
}
static class AutoConfigPostProcessor implements BeanFactoryPostProcessor, Ordered {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinition authManager = beanFactory.getBeanDefinition(AUTH_MANAGER_ID);
Map providerMap = beanFactory.getBeansOfType(AuthenticationProvider.class);
List providers = new ArrayList(providerMap.values());
// TODO: Sort providers according to defined ordering
authManager.getPropertyValues().addPropertyValue("providers", providers);
}
public int getOrder() {
return HIGHEST_PRECEDENCE;
}
}
}

View File

@ -1,15 +1,6 @@
package org.springframework.security.config;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.security.util.FilterChainProxy;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
/**
* Registers the bean definition parsers for the "security" namespace (http://www.springframework.org/schema/security).
@ -17,30 +8,15 @@ import org.w3c.dom.Element;
* @author Luke Taylor
* @version $Id$
*/
public class SecurityNamespaceHandler extends NamespaceHandlerSupport {
public static final String DEFAULT_FILTER_CHAIN_PROXY_ID = "_filterChainProxy";
public class SecurityNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("ldap", new LdapBeanDefinitionParser());
registerBeanDefinitionParser("http", new HttpSecurityBeanDefinitionParser());
registerBeanDefinitionParser("user-service", new UserServiceBeanDefinitionParser());
registerBeanDefinitionParser("authentication-provider", new AuthenticationProviderBeanDefinitionParser());
registerBeanDefinitionParser("autoconfig", new AutoConfigBeanDefinitionParser());
registerBeanDefinitionDecorator("intercept-methods", new InterceptMethodsBeanDefinitionDecorator());
registerBeanDefinitionDecorator("filter-chain-map", new FilterChainMapBeanDefinitionDecorator());
}
// private class HttpSecurityBeanDefinitionParser implements BeanDefinitionParser {
//
// public BeanDefinition parse(Element element, ParserContext parserContext) {
// RootBeanDefinition filterChainProxy = new RootBeanDefinition(FilterChainProxy.class);
//
// Element formLoginElt = DomUtils.getChildElementByTagName(element, "form-login");
//
// if (formLoginElt != null) {
// parse(formLoginElt, parserContext);
// }
//
// return filterChainProxy;
// }
// }
}

View File

@ -22,6 +22,8 @@ log4j.rootLogger=WARN, stdout, fileout
#log4j.logger.org.springframework.security.providers.dao=DEBUG, stdout, fileout
log4j.logger.sample.contact=DEBUG, stdout, fileout
log4j.logger.org.springframework.security=DEBUG, stdout, fileout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout