diff --git a/config/src/main/java/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessor.java b/config/src/main/java/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessor.java index 5edb615c0d..0ae6b55b25 100644 --- a/config/src/main/java/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessor.java +++ b/config/src/main/java/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessor.java @@ -52,7 +52,16 @@ final class AutowireBeanFactoryObjectPostProcessor implements ObjectPostProcesso */ @SuppressWarnings("unchecked") public T postProcess(T object) { - T result = (T) autowireBeanFactory.initializeBean(object, null); + if(object == null) { + return null; + } + T result = null; + try { + result = (T) autowireBeanFactory.initializeBean(object, object.toString()); + } catch (RuntimeException e) { + Class type = object.getClass(); + throw new RuntimeException("Could not postProcess " + object + " of type " + type, e); + } if(result instanceof DisposableBean) { disposableBeans.add((DisposableBean) result); } diff --git a/config/src/test/groovy/org/springframework/security/config/annotation/configuration/AroundMethodInterceptor.java b/config/src/test/groovy/org/springframework/security/config/annotation/configuration/AroundMethodInterceptor.java new file mode 100644 index 0000000000..7bd85edda2 --- /dev/null +++ b/config/src/test/groovy/org/springframework/security/config/annotation/configuration/AroundMethodInterceptor.java @@ -0,0 +1,25 @@ +/* + * Copyright 2002-2013 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.annotation.configuration; + +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; + +public class AroundMethodInterceptor implements MethodInterceptor { + public Object invoke(MethodInvocation methodInvocation) throws Throwable { + return String.valueOf(methodInvocation.proceed()); + } +} diff --git a/config/src/test/groovy/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessorTests.groovy b/config/src/test/groovy/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessorTests.groovy index a602f27991..1348b1f203 100644 --- a/config/src/test/groovy/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessorTests.groovy +++ b/config/src/test/groovy/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessorTests.groovy @@ -15,25 +15,23 @@ */ package org.springframework.security.config.annotation.configuration -import javax.servlet.ServletConfig -import javax.servlet.ServletContext - import org.springframework.beans.factory.BeanClassLoaderAware import org.springframework.beans.factory.BeanFactoryAware -import org.springframework.beans.factory.BeanNameAware -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.config.AutowireCapableBeanFactory; +import org.springframework.beans.factory.DisposableBean +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.config.AutowireCapableBeanFactory +import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.context.ApplicationContextAware import org.springframework.context.ApplicationEventPublisherAware import org.springframework.context.EnvironmentAware import org.springframework.context.MessageSourceAware import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration +import org.springframework.context.support.ClassPathXmlApplicationContext import org.springframework.mock.web.MockServletConfig import org.springframework.mock.web.MockServletContext import org.springframework.security.config.annotation.BaseSpringSpec -import org.springframework.security.config.annotation.ObjectPostProcessor; -import org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor; +import org.springframework.security.config.annotation.ObjectPostProcessor import org.springframework.web.context.ServletConfigAware import org.springframework.web.context.ServletContextAware import org.springframework.web.context.support.AnnotationConfigWebApplicationContext @@ -117,4 +115,27 @@ class AutowireBeanFactoryObjectPostProcessorTests extends BaseSpringSpec { return new AutowireBeanFactoryObjectPostProcessor(beanFactory); } } + + def "SEC-2382: AutowireBeanFactoryObjectPostProcessor works with BeanNameAutoProxyCreator"() { + when: + // must load with XML for BeanPostProcessors to work + context = new ClassPathXmlApplicationContext("AutowireBeanFactoryObjectPostProcessorTests-aopconfig.xml", getClass()); + then: + noExceptionThrown() + and: "make sure autoproxying was actually enabled" + context.getBean(MyAdvisedBean).doStuff() == "null" + } + + @Configuration + static class WithBanNameAutoProxyCreatorConfig { + @Bean + public ObjectPostProcessor objectPostProcessor(AutowireCapableBeanFactory beanFactory) { + return new AutowireBeanFactoryObjectPostProcessor(beanFactory) + } + + @Autowired + public void configure(ObjectPostProcessor p) { + p.postProcess(new Object()) + } + } } diff --git a/config/src/test/groovy/org/springframework/security/config/annotation/configuration/MyAdvisedBean.java b/config/src/test/groovy/org/springframework/security/config/annotation/configuration/MyAdvisedBean.java new file mode 100644 index 0000000000..1b0352b462 --- /dev/null +++ b/config/src/test/groovy/org/springframework/security/config/annotation/configuration/MyAdvisedBean.java @@ -0,0 +1,23 @@ +/* + * Copyright 2002-2013 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.annotation.configuration; + +public class MyAdvisedBean { + + public Object doStuff() { + return null; + } +} diff --git a/config/src/test/resources/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessorTests-aopconfig.xml b/config/src/test/resources/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessorTests-aopconfig.xml new file mode 100644 index 0000000000..d8f52e8d6c --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessorTests-aopconfig.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + +