|
@@ -3,8 +3,6 @@ package org.springframework.security.config;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
-import org.apache.commons.logging.Log;
|
|
|
-import org.apache.commons.logging.LogFactory;
|
|
|
import org.springframework.beans.BeanMetadataElement;
|
|
|
import org.springframework.beans.MutablePropertyValues;
|
|
|
import org.springframework.beans.PropertyValue;
|
|
@@ -15,6 +13,7 @@ import org.springframework.beans.factory.support.ManagedList;
|
|
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
|
|
import org.springframework.beans.factory.xml.ParserContext;
|
|
|
import org.springframework.security.afterinvocation.AfterInvocationProviderManager;
|
|
|
+import org.springframework.security.expression.support.MethodExpressionVoter;
|
|
|
import org.springframework.security.util.UrlUtils;
|
|
|
import org.springframework.security.vote.AffirmativeBased;
|
|
|
import org.springframework.security.vote.AuthenticatedVoter;
|
|
@@ -29,45 +28,54 @@ import org.w3c.dom.Element;
|
|
|
* @author Ben Alex
|
|
|
* @version $Id$
|
|
|
*/
|
|
|
-public abstract class ConfigUtils {
|
|
|
- private static final Log logger = LogFactory.getLog(ConfigUtils.class);
|
|
|
+abstract class ConfigUtils {
|
|
|
|
|
|
- static void registerDefaultAccessManagerIfNecessary(ParserContext parserContext) {
|
|
|
-
|
|
|
- if (!parserContext.getRegistry().containsBeanDefinition(BeanIds.ACCESS_MANAGER)) {
|
|
|
- ManagedList defaultVoters = new ManagedList(2);
|
|
|
+ static void registerDefaultWebAccessManagerIfNecessary(ParserContext parserContext) {
|
|
|
+ if (!parserContext.getRegistry().containsBeanDefinition(BeanIds.WEB_ACCESS_MANAGER)) {
|
|
|
+ parserContext.getRegistry().registerBeanDefinition(BeanIds.WEB_ACCESS_MANAGER,
|
|
|
+ createAccessManagerBean(RoleVoter.class, AuthenticatedVoter.class));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- defaultVoters.add(new RootBeanDefinition(RoleVoter.class));
|
|
|
- defaultVoters.add(new RootBeanDefinition(AuthenticatedVoter.class));
|
|
|
+ static void registerDefaultMethodAccessManagerIfNecessary(ParserContext parserContext) {
|
|
|
+ if (!parserContext.getRegistry().containsBeanDefinition(BeanIds.METHOD_ACCESS_MANAGER)) {
|
|
|
+ parserContext.getRegistry().registerBeanDefinition(BeanIds.METHOD_ACCESS_MANAGER,
|
|
|
+ createAccessManagerBean(MethodExpressionVoter.class, RoleVoter.class, AuthenticatedVoter.class));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- BeanDefinitionBuilder accessMgrBuilder = BeanDefinitionBuilder.rootBeanDefinition(AffirmativeBased.class);
|
|
|
- accessMgrBuilder.addPropertyValue("decisionVoters", defaultVoters);
|
|
|
- BeanDefinition accessMgr = accessMgrBuilder.getBeanDefinition();
|
|
|
+ private static BeanDefinition createAccessManagerBean(Class... voters) {
|
|
|
+ ManagedList defaultVoters = new ManagedList(voters.length);
|
|
|
|
|
|
- parserContext.getRegistry().registerBeanDefinition(BeanIds.ACCESS_MANAGER, accessMgr);
|
|
|
+ for(Class voter : voters) {
|
|
|
+ defaultVoters.add(new RootBeanDefinition(voter));
|
|
|
}
|
|
|
+
|
|
|
+ BeanDefinitionBuilder accessMgrBuilder = BeanDefinitionBuilder.rootBeanDefinition(AffirmativeBased.class);
|
|
|
+ accessMgrBuilder.addPropertyValue("decisionVoters", defaultVoters);
|
|
|
+ return accessMgrBuilder.getBeanDefinition();
|
|
|
}
|
|
|
-
|
|
|
- public static int countNonEmpty(String[] objects) {
|
|
|
- int nonNulls = 0;
|
|
|
-
|
|
|
- for (int i = 0; i < objects.length; i++) {
|
|
|
- if (StringUtils.hasText(objects[i])) {
|
|
|
- nonNulls++;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return nonNulls;
|
|
|
+
|
|
|
+ public static int countNonEmpty(String[] objects) {
|
|
|
+ int nonNulls = 0;
|
|
|
+
|
|
|
+ for (int i = 0; i < objects.length; i++) {
|
|
|
+ if (StringUtils.hasText(objects[i])) {
|
|
|
+ nonNulls++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nonNulls;
|
|
|
}
|
|
|
|
|
|
- public static void addVoter(BeanDefinition voter, ParserContext parserContext) {
|
|
|
- registerDefaultAccessManagerIfNecessary(parserContext);
|
|
|
+ static void addVoter(BeanDefinition voter, ParserContext parserContext) {
|
|
|
+ registerDefaultMethodAccessManagerIfNecessary(parserContext);
|
|
|
|
|
|
- BeanDefinition accessMgr = parserContext.getRegistry().getBeanDefinition(BeanIds.ACCESS_MANAGER);
|
|
|
+ BeanDefinition accessMgr = parserContext.getRegistry().getBeanDefinition(BeanIds.METHOD_ACCESS_MANAGER);
|
|
|
|
|
|
ManagedList voters = (ManagedList) accessMgr.getPropertyValues().getPropertyValue("decisionVoters").getValue();
|
|
|
voters.add(voter);
|
|
|
-
|
|
|
+
|
|
|
accessMgr.getPropertyValues().addPropertyValue("decisionVoters", voters);
|
|
|
}
|
|
|
|
|
@@ -92,12 +100,12 @@ public abstract class ConfigUtils {
|
|
|
BeanDefinition authManager = parserContext.getRegistry().getBeanDefinition(BeanIds.AUTHENTICATION_MANAGER);
|
|
|
((ArrayList) authManager.getPropertyValues().getPropertyValue("providerBeanNames").getValue()).add(beanName);
|
|
|
}
|
|
|
-
|
|
|
- static ManagedList getRegisteredAfterInvocationProviders(ParserContext parserContext) {
|
|
|
- BeanDefinition manager = registerAfterInvocationProviderManagerIfNecessary(parserContext);
|
|
|
- return (ManagedList) manager.getPropertyValues().getPropertyValue("providers").getValue();
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
+ static ManagedList getRegisteredAfterInvocationProviders(ParserContext parserContext) {
|
|
|
+ BeanDefinition manager = registerAfterInvocationProviderManagerIfNecessary(parserContext);
|
|
|
+ return (ManagedList) manager.getPropertyValues().getPropertyValue("providers").getValue();
|
|
|
+ }
|
|
|
+
|
|
|
private static BeanDefinition registerAfterInvocationProviderManagerIfNecessary(ParserContext parserContext) {
|
|
|
if(parserContext.getRegistry().containsBeanDefinition(BeanIds.AFTER_INVOCATION_MANAGER)) {
|
|
|
return parserContext.getRegistry().getBeanDefinition(BeanIds.AFTER_INVOCATION_MANAGER);
|
|
@@ -108,12 +116,12 @@ public abstract class ConfigUtils {
|
|
|
parserContext.getRegistry().registerBeanDefinition(BeanIds.AFTER_INVOCATION_MANAGER, manager);
|
|
|
|
|
|
return manager;
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- private static void registerFilterChainPostProcessorIfNecessary(ParserContext pc) {
|
|
|
- if (pc.getRegistry().containsBeanDefinition(BeanIds.FILTER_CHAIN_POST_PROCESSOR)) {
|
|
|
- return;
|
|
|
- }
|
|
|
+ private static void registerFilterChainPostProcessorIfNecessary(ParserContext pc) {
|
|
|
+ if (pc.getRegistry().containsBeanDefinition(BeanIds.FILTER_CHAIN_POST_PROCESSOR)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
// Post processor specifically to assemble and order the filter chain immediately before the FilterChainProxy is initialized.
|
|
|
RootBeanDefinition filterChainPostProcessor = new RootBeanDefinition(FilterChainProxyPostProcessor.class);
|
|
|
filterChainPostProcessor.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
|
@@ -122,62 +130,62 @@ public abstract class ConfigUtils {
|
|
|
filterList.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
|
|
pc.getRegistry().registerBeanDefinition(BeanIds.FILTER_LIST, filterList);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
static void addHttpFilter(ParserContext pc, BeanMetadataElement filter) {
|
|
|
- registerFilterChainPostProcessorIfNecessary(pc);
|
|
|
-
|
|
|
- RootBeanDefinition filterList = (RootBeanDefinition) pc.getRegistry().getBeanDefinition(BeanIds.FILTER_LIST);
|
|
|
-
|
|
|
- ManagedList filters;
|
|
|
- MutablePropertyValues pvs = filterList.getPropertyValues();
|
|
|
- if (pvs.contains("filters")) {
|
|
|
- filters = (ManagedList) pvs.getPropertyValue("filters").getValue();
|
|
|
- } else {
|
|
|
- filters = new ManagedList();
|
|
|
- pvs.addPropertyValue("filters", filters);
|
|
|
- }
|
|
|
-
|
|
|
- filters.add(filter);
|
|
|
+ registerFilterChainPostProcessorIfNecessary(pc);
|
|
|
+
|
|
|
+ RootBeanDefinition filterList = (RootBeanDefinition) pc.getRegistry().getBeanDefinition(BeanIds.FILTER_LIST);
|
|
|
+
|
|
|
+ ManagedList filters;
|
|
|
+ MutablePropertyValues pvs = filterList.getPropertyValues();
|
|
|
+ if (pvs.contains("filters")) {
|
|
|
+ filters = (ManagedList) pvs.getPropertyValue("filters").getValue();
|
|
|
+ } else {
|
|
|
+ filters = new ManagedList();
|
|
|
+ pvs.addPropertyValue("filters", filters);
|
|
|
+ }
|
|
|
+
|
|
|
+ filters.add(filter);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Bean which holds the list of filters which are maintained in the context and modified by calls to
|
|
|
+ * Bean which holds the list of filters which are maintained in the context and modified by calls to
|
|
|
* addHttpFilter. The post processor retrieves these before injecting the list into the FilterChainProxy.
|
|
|
*/
|
|
|
public static class FilterChainList {
|
|
|
- List filters;
|
|
|
+ List filters;
|
|
|
|
|
|
- public List getFilters() {
|
|
|
- return filters;
|
|
|
- }
|
|
|
+ public List getFilters() {
|
|
|
+ return filters;
|
|
|
+ }
|
|
|
|
|
|
- public void setFilters(List filters) {
|
|
|
- this.filters = filters;
|
|
|
- }
|
|
|
+ public void setFilters(List filters) {
|
|
|
+ this.filters = filters;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Checks the value of an XML attribute which represents a redirect URL.
|
|
|
- * If not empty or starting with "$" (potential placeholder), "/" or "http" it will raise an error.
|
|
|
+ * If not empty or starting with "$" (potential placeholder), "/" or "http" it will raise an error.
|
|
|
*/
|
|
|
static void validateHttpRedirect(String url, ParserContext pc, Object source) {
|
|
|
- if (UrlUtils.isValidRedirectUrl(url) || url.startsWith("$")) {
|
|
|
- return;
|
|
|
- }
|
|
|
- pc.getReaderContext().warning(url + " is not a valid redirect URL (must start with '/' or http(s))", source);
|
|
|
+ if (UrlUtils.isValidRedirectUrl(url) || url.startsWith("$")) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ pc.getReaderContext().warning(url + " is not a valid redirect URL (must start with '/' or http(s))", source);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
static void setSessionControllerOnAuthenticationManager(ParserContext pc, String beanName, Element sourceElt) {
|
|
|
- registerProviderManagerIfNecessary(pc);
|
|
|
- BeanDefinition authManager = pc.getRegistry().getBeanDefinition(BeanIds.AUTHENTICATION_MANAGER);
|
|
|
+ registerProviderManagerIfNecessary(pc);
|
|
|
+ BeanDefinition authManager = pc.getRegistry().getBeanDefinition(BeanIds.AUTHENTICATION_MANAGER);
|
|
|
PropertyValue pv = authManager.getPropertyValues().getPropertyValue("sessionController");
|
|
|
-
|
|
|
+
|
|
|
if (pv != null && pv.getValue() != null) {
|
|
|
- pc.getReaderContext().error("A session controller has already been set on the authentication manager. " +
|
|
|
- "The <concurrent-session-control> element isn't compatible with a custom session controller",
|
|
|
- pc.extractSource(sourceElt));
|
|
|
+ pc.getReaderContext().error("A session controller has already been set on the authentication manager. " +
|
|
|
+ "The <concurrent-session-control> element isn't compatible with a custom session controller",
|
|
|
+ pc.extractSource(sourceElt));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
authManager.getPropertyValues().addPropertyValue("sessionController", new RuntimeBeanReference(beanName));
|
|
|
}
|
|
|
}
|