Moved registration of ProviderManager bean to ConfigUtils.
This commit is contained in:
parent
06ce4b79e9
commit
0185dc5a90
|
@ -15,28 +15,14 @@ import org.w3c.dom.Element;
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
class AuthenticationProviderBeanDefinitionParser implements BeanDefinitionParser {
|
class AuthenticationProviderBeanDefinitionParser implements BeanDefinitionParser {
|
||||||
public static final String DEFAULT_AUTH_MANAGER_ID = "_authenticationManager";
|
|
||||||
|
|
||||||
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) {
|
private ManagedList getRegisteredProviders(ParserContext parserContext) {
|
||||||
BeanDefinition authManager = registerProviderManagerIfNecessary(parserContext);
|
BeanDefinition authManager = ConfigUtils.registerProviderManagerIfNecessary(parserContext);
|
||||||
return (ManagedList) authManager.getPropertyValues().getPropertyValue("providers").getValue();
|
return (ManagedList) authManager.getPropertyValues().getPropertyValue("providers").getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||||
registerProviderManagerIfNecessary(parserContext);
|
ConfigUtils.registerProviderManagerIfNecessary(parserContext);
|
||||||
|
|
||||||
RootBeanDefinition authProvider;
|
RootBeanDefinition authProvider;
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,12 @@ import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
|
import org.springframework.beans.factory.support.ManagedList;
|
||||||
|
import org.springframework.beans.factory.xml.ParserContext;
|
||||||
import org.springframework.security.AccessDecisionManager;
|
import org.springframework.security.AccessDecisionManager;
|
||||||
import org.springframework.security.AuthenticationManager;
|
import org.springframework.security.AuthenticationManager;
|
||||||
|
import org.springframework.security.providers.ProviderManager;
|
||||||
import org.springframework.security.vote.AffirmativeBased;
|
import org.springframework.security.vote.AffirmativeBased;
|
||||||
import org.springframework.security.vote.AuthenticatedVoter;
|
import org.springframework.security.vote.AuthenticatedVoter;
|
||||||
import org.springframework.security.vote.RoleVoter;
|
import org.springframework.security.vote.RoleVoter;
|
||||||
|
@ -15,19 +19,22 @@ import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Utitily methods used internally by the Spring Security namespace configuration code.
|
||||||
|
*
|
||||||
* @author Luke Taylor
|
* @author Luke Taylor
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public abstract class ConfigUtils {
|
public abstract class ConfigUtils {
|
||||||
public static final String DEFAULT_ACCESS_MANAGER_ID = "_accessManager";
|
public static final String DEFAULT_ACCESS_MANAGER_ID = "_accessManager";
|
||||||
|
public static final String DEFAULT_AUTH_MANAGER_ID = "_authenticationManager";
|
||||||
|
|
||||||
static void registerAccessManagerIfNecessary(ConfigurableListableBeanFactory bf) {
|
static void registerAccessManagerIfNecessary(ConfigurableListableBeanFactory bf) {
|
||||||
if (bf.getBeanNamesForType(AccessDecisionManager.class).length > 0) {
|
if (bf.getBeanNamesForType(AccessDecisionManager.class).length > 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Assert.isInstanceOf(BeanDefinitionRegistry.class, bf, " Auto-registration of default AccessManager will only work " +
|
Assert.isInstanceOf(BeanDefinitionRegistry.class, bf, " Auto-registration of default AccessManager will " +
|
||||||
"with a BeanFactory which implements BeanDefinitionRegistry");
|
"only work with a BeanFactory which implements BeanDefinitionRegistry");
|
||||||
|
|
||||||
BeanDefinitionRegistry registry = (BeanDefinitionRegistry)bf;
|
BeanDefinitionRegistry registry = (BeanDefinitionRegistry)bf;
|
||||||
|
|
||||||
|
@ -41,22 +48,32 @@ public abstract class ConfigUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static AuthenticationManager getAuthenticationManager(ConfigurableListableBeanFactory bf) {
|
/**
|
||||||
Map authManagers = bf.getBeansOfType(AuthenticationManager.class);
|
* Creates and registers the bean definition for the default ProviderManager instance and returns
|
||||||
|
* the BeanDefinition for it. This method will typically be called when registering authentication providers
|
||||||
if (authManagers.size() == 0) {
|
* using the <security:provider /> tag or by other beans which have a dependency on the
|
||||||
throw new IllegalArgumentException("No AuthenticationManager registered. " +
|
* authentication manager.
|
||||||
"Make sure you have configured at least one AuthenticationProvider?");
|
*/
|
||||||
|
static BeanDefinition registerProviderManagerIfNecessary(ParserContext parserContext) {
|
||||||
} else if (authManagers.size() > 1) {
|
if(parserContext.getRegistry().containsBeanDefinition(DEFAULT_AUTH_MANAGER_ID)) {
|
||||||
throw new IllegalArgumentException("More than one AuthenticationManager registered.");
|
return parserContext.getRegistry().getBeanDefinition(DEFAULT_AUTH_MANAGER_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
AuthenticationManager accessMgr = (AuthenticationManager) authManagers.values().toArray()[0];
|
BeanDefinition authManager = new RootBeanDefinition(ProviderManager.class);
|
||||||
|
authManager.getPropertyValues().addPropertyValue("providers", new ManagedList());
|
||||||
|
parserContext.getRegistry().registerBeanDefinition(DEFAULT_AUTH_MANAGER_ID, authManager);
|
||||||
|
|
||||||
return accessMgr;
|
return authManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supplies the BeanDefinition for an instance of AbstractSecurityInterceptor with the default
|
||||||
|
* AccessDecisionManager and AuthenticationManager.
|
||||||
|
*
|
||||||
|
* @param beanFactory
|
||||||
|
* @param securityInterceptor
|
||||||
|
*/
|
||||||
static void configureSecurityInterceptor(ConfigurableListableBeanFactory beanFactory,
|
static void configureSecurityInterceptor(ConfigurableListableBeanFactory beanFactory,
|
||||||
BeanDefinition securityInterceptor) {
|
BeanDefinition securityInterceptor) {
|
||||||
|
|
||||||
|
@ -75,4 +92,21 @@ public abstract class ConfigUtils {
|
||||||
securityInterceptor.getPropertyValues().addPropertyValue("authenticationManager",
|
securityInterceptor.getPropertyValues().addPropertyValue("authenticationManager",
|
||||||
getAuthenticationManager(beanFactory));
|
getAuthenticationManager(beanFactory));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static AuthenticationManager getAuthenticationManager(ConfigurableListableBeanFactory bf) {
|
||||||
|
Map authManagers = bf.getBeansOfType(AuthenticationManager.class);
|
||||||
|
|
||||||
|
if (authManagers.size() == 0) {
|
||||||
|
throw new IllegalArgumentException("No AuthenticationManager registered. " +
|
||||||
|
"Make sure you have configured at least one AuthenticationProvider?");
|
||||||
|
|
||||||
|
} else if (authManagers.size() > 1) {
|
||||||
|
throw new IllegalArgumentException("More than one AuthenticationManager registered.");
|
||||||
|
}
|
||||||
|
|
||||||
|
AuthenticationManager accessMgr = (AuthenticationManager) authManagers.values().toArray()[0];
|
||||||
|
|
||||||
|
return accessMgr;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue