From 4cdeacc277c0a829694c46897bcfec6163a8e520 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Thu, 6 Mar 2014 20:51:24 -0600 Subject: [PATCH] SEC-2499: Allow MethodSecurityExpressionHandler in parent context Previously a NoSuchBeanDefintionException was thrown when the MethodSecurityExpressionHandler was defined in the parent context. This happened due to trying to work around ordering issues related to SEC-2136 This commit resolves this by not marking the MethodSecurityExpressionHandler bean as lazy unless it exists. --- ...balMethodSecurityBeanDefinitionParser.java | 3 ++ .../config/method/sec2499/Sec2499Tests.java | 50 +++++++++++++++++++ .../security/config/method/sec2499/child.xml | 12 +++++ .../security/config/method/sec2499/parent.xml | 9 ++++ 4 files changed, 74 insertions(+) create mode 100644 config/src/test/java/org/springframework/security/config/method/sec2499/Sec2499Tests.java create mode 100644 config/src/test/resources/org/springframework/security/config/method/sec2499/child.xml create mode 100644 config/src/test/resources/org/springframework/security/config/method/sec2499/parent.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 217d5b984d..93158b4800 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 @@ -452,6 +452,9 @@ public class GlobalMethodSecurityBeanDefinitionParser implements BeanDefinitionP } public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { + if(!registry.containsBeanDefinition(beanName)) { + return; + } BeanDefinition beanDefinition = registry.getBeanDefinition(beanName); beanDefinition.setLazyInit(true); } diff --git a/config/src/test/java/org/springframework/security/config/method/sec2499/Sec2499Tests.java b/config/src/test/java/org/springframework/security/config/method/sec2499/Sec2499Tests.java new file mode 100644 index 0000000000..ae8a13905e --- /dev/null +++ b/config/src/test/java/org/springframework/security/config/method/sec2499/Sec2499Tests.java @@ -0,0 +1,50 @@ +/* + * Copyright 2002-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.config.method.sec2499; + +import org.junit.After; +import org.junit.Test; +import org.springframework.context.support.GenericXmlApplicationContext; + +/** + * + * @author Rob Winch + * + */ +public class Sec2499Tests { + private GenericXmlApplicationContext parent; + + private GenericXmlApplicationContext child; + + @After + public void cleanup() { + if(parent != null) { + parent.close(); + } + if(child != null) { + child.close(); + } + } + + @Test + public void methodExpressionHandlerInParentContextLoads() { + parent = new GenericXmlApplicationContext("org/springframework/security/config/method/sec2499/parent.xml"); + child = new GenericXmlApplicationContext(); + child.load("org/springframework/security/config/method/sec2499/child.xml"); + child.setParent(parent); + child.refresh(); + } +} \ No newline at end of file diff --git a/config/src/test/resources/org/springframework/security/config/method/sec2499/child.xml b/config/src/test/resources/org/springframework/security/config/method/sec2499/child.xml new file mode 100644 index 0000000000..b9bc511cdb --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/method/sec2499/child.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/config/src/test/resources/org/springframework/security/config/method/sec2499/parent.xml b/config/src/test/resources/org/springframework/security/config/method/sec2499/parent.xml new file mode 100644 index 0000000000..cf62a87ce0 --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/method/sec2499/parent.xml @@ -0,0 +1,9 @@ + + + + + +