From a421370a3dd736c602ea68a8389dc50d08574700 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Sun, 25 Apr 2010 22:00:25 +0100 Subject: [PATCH] SEC-1465: Change DelegatingMethodSecurityMetadataSource to use constructor injection to get round the problem of it being invoked before it has been initialized properly. Also changed the contacts tests to use the same app context and loading order as the actual webapp, to give better reassurance that the app will run successfully. --- ...balMethodSecurityBeanDefinitionParser.java | 2 +- ...elegatingMethodSecurityMetadataSource.java | 19 +++++------ gradle/javaprojects.gradle | 2 +- .../applicationContext-security.xml | 2 +- .../contacts/src/main/webapp/WEB-INF/web.xml | 2 +- .../sample/contact/ContactManagerTests.java | 4 +-- .../applicationContext-contacts-test.xml | 34 ------------------- 7 files changed, 14 insertions(+), 51 deletions(-) rename samples/contacts/src/main/{webapp/WEB-INF => resources}/applicationContext-security.xml (99%) delete mode 100644 samples/contacts/src/test/resources/applicationContext-contacts-test.xml diff --git a/config/src/main/java/org/springframework/security/config/method/GlobalMethodSecurityBeanDefinitionParser.java b/config/src/main/java/org/springframework/security/config/method/GlobalMethodSecurityBeanDefinitionParser.java index b2f614c2ec..7af3ef0ed3 100644 --- a/config/src/main/java/org/springframework/security/config/method/GlobalMethodSecurityBeanDefinitionParser.java +++ b/config/src/main/java/org/springframework/security/config/method/GlobalMethodSecurityBeanDefinitionParser.java @@ -252,7 +252,7 @@ public class GlobalMethodSecurityBeanDefinitionParser implements BeanDefinitionP private BeanReference registerDelegatingMethodSecurityMetadataSource(ParserContext pc, ManagedList delegates, Object source) { RootBeanDefinition delegatingMethodSecurityMetadataSource = new RootBeanDefinition(DelegatingMethodSecurityMetadataSource.class); delegatingMethodSecurityMetadataSource.setSource(source); - delegatingMethodSecurityMetadataSource.getPropertyValues().addPropertyValue("methodSecurityMetadataSources", delegates); + delegatingMethodSecurityMetadataSource.getConstructorArgumentValues().addGenericArgumentValue(delegates); String id = pc.getReaderContext().generateBeanName(delegatingMethodSecurityMetadataSource); pc.registerBeanComponent(new BeanComponentDefinition(delegatingMethodSecurityMetadataSource, id)); diff --git a/core/src/main/java/org/springframework/security/access/method/DelegatingMethodSecurityMetadataSource.java b/core/src/main/java/org/springframework/security/access/method/DelegatingMethodSecurityMetadataSource.java index f2aeeab073..9c93cda437 100644 --- a/core/src/main/java/org/springframework/security/access/method/DelegatingMethodSecurityMetadataSource.java +++ b/core/src/main/java/org/springframework/security/access/method/DelegatingMethodSecurityMetadataSource.java @@ -9,7 +9,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.springframework.beans.factory.InitializingBean; import org.springframework.security.access.ConfigAttribute; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; @@ -21,19 +20,22 @@ import org.springframework.util.ObjectUtils; * @author Ben Alex * @author Luke Taylor */ -public final class DelegatingMethodSecurityMetadataSource extends AbstractMethodSecurityMetadataSource implements InitializingBean { +public final class DelegatingMethodSecurityMetadataSource extends AbstractMethodSecurityMetadataSource { private final static List NULL_CONFIG_ATTRIBUTE = Collections.emptyList(); - private List methodSecurityMetadataSources; + private final List methodSecurityMetadataSources; private final Map> attributeCache = new HashMap>(); - //~ Methods ======================================================================================================== + //~ Constructor ==================================================================================================== - public void afterPropertiesSet() throws Exception { - Assert.notNull(methodSecurityMetadataSources, "A list of MethodSecurityMetadataSources is required"); + public DelegatingMethodSecurityMetadataSource(List methodSecurityMetadataSources) { + Assert.notEmpty(methodSecurityMetadataSources, "MethodSecurityMetadataSources cannot be null or empty"); + this.methodSecurityMetadataSources = methodSecurityMetadataSources; } + //~ Methods ======================================================================================================== + public Collection getAttributes(Method method, Class targetClass) { DefaultCacheKey cacheKey = new DefaultCacheKey(method, targetClass); synchronized (attributeCache) { @@ -83,11 +85,6 @@ public final class DelegatingMethodSecurityMetadataSource extends AbstractMethod return set; } - @SuppressWarnings("unchecked") - public void setMethodSecurityMetadataSources(List methodSecurityMetadataSources) { - this.methodSecurityMetadataSources = methodSecurityMetadataSources; - } - //~ Inner Classes ================================================================================================== private static class DefaultCacheKey { diff --git a/gradle/javaprojects.gradle b/gradle/javaprojects.gradle index 3b729e4fda..23d4947f97 100644 --- a/gradle/javaprojects.gradle +++ b/gradle/javaprojects.gradle @@ -1,7 +1,7 @@ apply plugin: 'java' apply plugin: 'eclipse' -springVersion = '3.0.1.RELEASE' +springVersion = '3.0.2.RELEASE' springLdapVersion = '1.3.0.RELEASE' ehcacheVersion = '1.6.2' aspectjVersion = '1.6.8' diff --git a/samples/contacts/src/main/webapp/WEB-INF/applicationContext-security.xml b/samples/contacts/src/main/resources/applicationContext-security.xml similarity index 99% rename from samples/contacts/src/main/webapp/WEB-INF/applicationContext-security.xml rename to samples/contacts/src/main/resources/applicationContext-security.xml index 957670168a..5a952f205d 100644 --- a/samples/contacts/src/main/webapp/WEB-INF/applicationContext-security.xml +++ b/samples/contacts/src/main/resources/applicationContext-security.xml @@ -11,7 +11,7 @@ xmlns:b="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd - http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> + http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> diff --git a/samples/contacts/src/main/webapp/WEB-INF/web.xml b/samples/contacts/src/main/webapp/WEB-INF/web.xml index f1783027fa..8166f40687 100644 --- a/samples/contacts/src/main/webapp/WEB-INF/web.xml +++ b/samples/contacts/src/main/webapp/WEB-INF/web.xml @@ -18,9 +18,9 @@ contextConfigLocation - /WEB-INF/applicationContext-security.xml classpath:applicationContext-common-business.xml classpath:applicationContext-common-authorization.xml + classpath:applicationContext-security.xml diff --git a/samples/contacts/src/test/java/sample/contact/ContactManagerTests.java b/samples/contacts/src/test/java/sample/contact/ContactManagerTests.java index 339b85e28a..b90256fa53 100644 --- a/samples/contacts/src/test/java/sample/contact/ContactManagerTests.java +++ b/samples/contacts/src/test/java/sample/contact/ContactManagerTests.java @@ -40,9 +40,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * @Author Luke Taylor */ @ContextConfiguration(locations={ + "/applicationContext-security.xml", "/applicationContext-common-authorization.xml", - "/applicationContext-common-business.xml", - "/applicationContext-contacts-test.xml"}) + "/applicationContext-common-business.xml"}) @RunWith(SpringJUnit4ClassRunner.class) public class ContactManagerTests { //~ Instance fields ================================================================================================ diff --git a/samples/contacts/src/test/resources/applicationContext-contacts-test.xml b/samples/contacts/src/test/resources/applicationContext-contacts-test.xml deleted file mode 100644 index 2eb677db48..0000000000 --- a/samples/contacts/src/test/resources/applicationContext-contacts-test.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - -