Moved responsability for creating AuthenticationManager into AuthenticationProviderBeanDefinitionParser. Tidied up SecrityNamespaceHandler.
This commit is contained in:
parent
3927ba9ed0
commit
03e285c31d
|
@ -1,47 +1,58 @@
|
||||||
package org.springframework.security.config;
|
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.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.providers.dao.DaoAuthenticationProvider;
|
||||||
import org.springframework.security.ui.logout.LogoutFilter;
|
|
||||||
import org.springframework.util.xml.DomUtils;
|
import org.springframework.util.xml.DomUtils;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author luke
|
* @author Luke Taylor
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class AuthenticationProviderBeanDefinitionParser extends AbstractBeanDefinitionParser {
|
class AuthenticationProviderBeanDefinitionParser implements BeanDefinitionParser {
|
||||||
private static final String DEFAULT_PROVIDER_BEAN_ID = "_authenticationProvider";
|
public static final String DEFAULT_AUTH_MANAGER_ID = "_authenticationManager";
|
||||||
|
|
||||||
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
|
private BeanDefinition registerProviderManagerIfNecessary(ParserContext parserContext) {
|
||||||
RootBeanDefinition authProvider = new RootBeanDefinition(DaoAuthenticationProvider.class);
|
|
||||||
|
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
|
// TODO: Proper implementation
|
||||||
Element userServiceElt = DomUtils.getChildElementByTagName(element, "user-service");
|
Element userServiceElt = DomUtils.getChildElementByTagName(element, "user-service");
|
||||||
|
|
||||||
BeanDefinition userDetailsService = new UserServiceBeanDefinitionParser().parse(userServiceElt, parserContext);
|
if (userServiceElt != null) {
|
||||||
authProvider.getPropertyValues().addPropertyValue("userDetailsService", userDetailsService);
|
authProvider = new RootBeanDefinition(DaoAuthenticationProvider.class);
|
||||||
|
BeanDefinition userDetailsService = new UserServiceBeanDefinitionParser().parse(userServiceElt, parserContext);
|
||||||
return authProvider;
|
authProvider.getPropertyValues().addPropertyValue("userDetailsService", userDetailsService);
|
||||||
}
|
} else {
|
||||||
|
throw new IllegalArgumentException("Only support user-service provider at the moment.");
|
||||||
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) throws BeanDefinitionStoreException {
|
|
||||||
String id = super.resolveId(element, definition, parserContext);
|
|
||||||
|
|
||||||
if (StringUtils.hasText(id)) {
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Check for duplicate using default id here.
|
getRegisteredProviders(parserContext).add(authProvider);
|
||||||
|
|
||||||
return DEFAULT_PROVIDER_BEAN_ID;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,19 +22,14 @@ import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author luke
|
* @author Luke Taylor
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class AutoConfigBeanDefinitionParser implements BeanDefinitionParser {
|
public class AutoConfigBeanDefinitionParser implements BeanDefinitionParser {
|
||||||
public static final String AUTH_MANAGER_ID = "_authenticationManager";
|
|
||||||
public static final String DEFAULT_ACCESS_MANAGER_ID = "_accessManager";
|
public static final String DEFAULT_ACCESS_MANAGER_ID = "_accessManager";
|
||||||
|
|
||||||
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||||
RootBeanDefinition authenticationManager = new RootBeanDefinition(ProviderManager.class);
|
|
||||||
BeanDefinitionRegistry registry = parserContext.getRegistry();
|
BeanDefinitionRegistry registry = parserContext.getRegistry();
|
||||||
|
|
||||||
registry.registerBeanDefinition(AUTH_MANAGER_ID, authenticationManager);
|
|
||||||
registry.registerBeanDefinition("_autoconfigPP", new RootBeanDefinition(AutoConfigPostProcessor.class));
|
|
||||||
|
|
||||||
RootBeanDefinition accessManager = new RootBeanDefinition(AffirmativeBased.class);
|
RootBeanDefinition accessManager = new RootBeanDefinition(AffirmativeBased.class);
|
||||||
|
|
||||||
|
@ -44,22 +39,4 @@ public class AutoConfigBeanDefinitionParser implements BeanDefinitionParser {
|
||||||
registry.registerBeanDefinition(DEFAULT_ACCESS_MANAGER_ID, accessManager);
|
registry.registerBeanDefinition(DEFAULT_ACCESS_MANAGER_ID, accessManager);
|
||||||
return null;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,6 @@
|
||||||
package org.springframework.security.config;
|
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.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).
|
* 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
|
* @author Luke Taylor
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class SecurityNamespaceHandler extends NamespaceHandlerSupport {
|
public class SecurityNamespaceHandler extends NamespaceHandlerSupport {
|
||||||
public static final String DEFAULT_FILTER_CHAIN_PROXY_ID = "_filterChainProxy";
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
registerBeanDefinitionParser("ldap", new LdapBeanDefinitionParser());
|
registerBeanDefinitionParser("ldap", new LdapBeanDefinitionParser());
|
||||||
registerBeanDefinitionParser("http", new HttpSecurityBeanDefinitionParser());
|
registerBeanDefinitionParser("http", new HttpSecurityBeanDefinitionParser());
|
||||||
|
registerBeanDefinitionParser("user-service", new UserServiceBeanDefinitionParser());
|
||||||
registerBeanDefinitionParser("authentication-provider", new AuthenticationProviderBeanDefinitionParser());
|
registerBeanDefinitionParser("authentication-provider", new AuthenticationProviderBeanDefinitionParser());
|
||||||
registerBeanDefinitionParser("autoconfig", new AutoConfigBeanDefinitionParser());
|
registerBeanDefinitionParser("autoconfig", new AutoConfigBeanDefinitionParser());
|
||||||
registerBeanDefinitionDecorator("intercept-methods", new InterceptMethodsBeanDefinitionDecorator());
|
registerBeanDefinitionDecorator("intercept-methods", new InterceptMethodsBeanDefinitionDecorator());
|
||||||
registerBeanDefinitionDecorator("filter-chain-map", new FilterChainMapBeanDefinitionDecorator());
|
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;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,8 @@ log4j.rootLogger=WARN, stdout, fileout
|
||||||
#log4j.logger.org.springframework.security.providers.dao=DEBUG, stdout, fileout
|
#log4j.logger.org.springframework.security.providers.dao=DEBUG, stdout, fileout
|
||||||
log4j.logger.sample.contact=DEBUG, stdout, fileout
|
log4j.logger.sample.contact=DEBUG, stdout, fileout
|
||||||
|
|
||||||
|
log4j.logger.org.springframework.security=DEBUG, stdout, fileout
|
||||||
|
|
||||||
# Console output...
|
# Console output...
|
||||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||||
|
|
Loading…
Reference in New Issue