Sfoglia il codice sorgente

SEC-2382: AutowireBeanFactoryObjectPostProcessor works w/ BeanNameAutoProxyCreator

Rob Winch 12 anni fa
parent
commit
26be54653b

+ 10 - 1
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> 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);
         }

+ 25 - 0
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());
+    }
+}

+ 29 - 8
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<Object> p) {
+            p.postProcess(new Object())
+        }
+    }
 }

+ 23 - 0
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;
+    }
+}

+ 23 - 0
config/src/test/resources/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessorTests-aopconfig.xml

@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xmlns:aop="http://www.springframework.org/schema/aop"
+    xmlns:p="http://www.springframework.org/schema/p"
+    xmlns:context="http://www.springframework.org/schema/context"
+    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
+        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
+
+    <context:annotation-config/>
+
+    <bean class="org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessorTests$WithBanNameAutoProxyCreatorConfig"/>
+
+    <bean id="interceptor" class="org.springframework.security.config.annotation.configuration.AroundMethodInterceptor"/>
+    <bean id="myBean" class="org.springframework.security.config.annotation.configuration.MyAdvisedBean"/>
+    <bean id="autoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
+        p:beanNames="myBean"
+        p:interceptorNames="interceptor"/>
+       <bean id="pointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"
+           p:mappedName="doStuff"
+           p:advice-ref="interceptor"/>
+</beans>