mirror of
				https://github.com/spring-projects/spring-security.git
				synced 2025-10-30 22:28:46 +00:00 
			
		
		
		
	Added MethodDefinitionSourceAdvisor for performance and autoproxying.
This commit is contained in:
		
							parent
							
								
									740517c55b
								
							
						
					
					
						commit
						8d973af603
					
				| @ -1,3 +1,8 @@ | |||||||
|  | Changes in version 0.7 (2004-xx-xx) | ||||||
|  | ----------------------------------- | ||||||
|  | 
 | ||||||
|  | * Added MethodDefinitionSourceAdvisor for performance and autoproxying | ||||||
|  | 
 | ||||||
| Changes in version 0.6.1 (2004-09-25) | Changes in version 0.6.1 (2004-09-25) | ||||||
| ------------------------------------- | ------------------------------------- | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -0,0 +1,120 @@ | |||||||
|  | /* Copyright 2004 Acegi Technology Pty Limited | ||||||
|  |  * | ||||||
|  |  * 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 net.sf.acegisecurity.intercept.method; | ||||||
|  | 
 | ||||||
|  | import org.aopalliance.intercept.MethodInvocation; | ||||||
|  | 
 | ||||||
|  | import org.springframework.aop.framework.AopConfigException; | ||||||
|  | import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; | ||||||
|  | 
 | ||||||
|  | import java.lang.reflect.AccessibleObject; | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Advisor driven by a {@link MethodDefinitionSource}, used to exclude a {@link | ||||||
|  |  * MethodSecurityInterceptor} from public (ie non-secure) methods. | ||||||
|  |  *  | ||||||
|  |  * <p> | ||||||
|  |  * Because the AOP framework caches advice calculations, this is normally | ||||||
|  |  * faster than just letting the <code>MethodSecurityInterceptor</code> run and | ||||||
|  |  * find out itself that it has no work to do. | ||||||
|  |  * </p> | ||||||
|  |  *  | ||||||
|  |  * <p> | ||||||
|  |  * This class also allows the use of Spring's | ||||||
|  |  * <code>DefaultAdvisorAutoProxyCreator</code>, which makes configuration | ||||||
|  |  * easier than setup a <code>ProxyFactoryBean</code> for each object requiring | ||||||
|  |  * security. Note that autoproxying is not supported for BeanFactory | ||||||
|  |  * implementations, as post-processing is automatic only for application | ||||||
|  |  * contexts. | ||||||
|  |  * </p> | ||||||
|  |  *  | ||||||
|  |  * <p> | ||||||
|  |  * Based on Spring's TransactionAttributeSourceAdvisor. | ||||||
|  |  * </p> | ||||||
|  |  *  | ||||||
|  |  * @author Ben Alex | ||||||
|  |  * @version $Id$ | ||||||
|  |  */ | ||||||
|  | public class MethodDefinitionSourceAdvisor | ||||||
|  |     extends StaticMethodMatcherPointcutAdvisor { | ||||||
|  |     //~ Instance fields ======================================================== | ||||||
|  | 
 | ||||||
|  |     private MethodDefinitionSource transactionAttributeSource; | ||||||
|  | 
 | ||||||
|  |     //~ Constructors =========================================================== | ||||||
|  | 
 | ||||||
|  |     public MethodDefinitionSourceAdvisor(MethodSecurityInterceptor advice) { | ||||||
|  |         super(advice); | ||||||
|  | 
 | ||||||
|  |         if (advice.getObjectDefinitionSource() == null) { | ||||||
|  |             throw new AopConfigException( | ||||||
|  |                 "Cannot construct a MethodDefinitionSourceAdvisor using a " | ||||||
|  |                 + "MethodSecurityInterceptor that has no ObjectDefinitionSource configured"); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         this.transactionAttributeSource = advice.getObjectDefinitionSource(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     //~ Methods ================================================================ | ||||||
|  | 
 | ||||||
|  |     public boolean matches(Method m, Class targetClass) { | ||||||
|  |         MethodInvocation methodInvocation = new InternalMethodInvocation(m); | ||||||
|  | 
 | ||||||
|  |         return (this.transactionAttributeSource.getAttributes(methodInvocation) != null); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     //~ Inner Classes ========================================================== | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Represents a <code>MethodInvocation</code>. | ||||||
|  |      *  | ||||||
|  |      * <p> | ||||||
|  |      * Required as <code>MethodDefinitionSource</code> only supports lookup of | ||||||
|  |      * configuration attributes for <code>MethodInvocation</code>s. | ||||||
|  |      * </p> | ||||||
|  |      */ | ||||||
|  |     private class InternalMethodInvocation implements MethodInvocation { | ||||||
|  |         Method method; | ||||||
|  | 
 | ||||||
|  |         public InternalMethodInvocation(Method method) { | ||||||
|  |             this.method = method; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private InternalMethodInvocation() {} | ||||||
|  | 
 | ||||||
|  |         public Object[] getArguments() { | ||||||
|  |             throw new UnsupportedOperationException(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public Method getMethod() { | ||||||
|  |             return this.method; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public AccessibleObject getStaticPart() { | ||||||
|  |             throw new UnsupportedOperationException(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public Object getThis() { | ||||||
|  |             throw new UnsupportedOperationException(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public Object proceed() throws Throwable { | ||||||
|  |             throw new UnsupportedOperationException(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,98 @@ | |||||||
|  | /* Copyright 2004 Acegi Technology Pty Limited | ||||||
|  |  * | ||||||
|  |  * 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 net.sf.acegisecurity.intercept.method; | ||||||
|  | 
 | ||||||
|  | import junit.framework.TestCase; | ||||||
|  | 
 | ||||||
|  | import net.sf.acegisecurity.TargetObject; | ||||||
|  | 
 | ||||||
|  | import org.springframework.aop.framework.AopConfigException; | ||||||
|  | 
 | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Tests {@link MethodDefinitionSourceAdvisor}. | ||||||
|  |  * | ||||||
|  |  * @author Ben Alex | ||||||
|  |  * @version $Id$ | ||||||
|  |  */ | ||||||
|  | public class MethodDefinitionSourceAdvisorTests extends TestCase { | ||||||
|  |     //~ Constructors =========================================================== | ||||||
|  | 
 | ||||||
|  |     public MethodDefinitionSourceAdvisorTests() { | ||||||
|  |         super(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public MethodDefinitionSourceAdvisorTests(String arg0) { | ||||||
|  |         super(arg0); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     //~ Methods ================================================================ | ||||||
|  | 
 | ||||||
|  |     public final void setUp() throws Exception { | ||||||
|  |         super.setUp(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static void main(String[] args) { | ||||||
|  |         junit.textui.TestRunner.run(MethodDefinitionSourceAdvisorTests.class); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void testAdvisorReturnsFalseWhenMethodInvocationNotDefined() | ||||||
|  |         throws Exception { | ||||||
|  |         Class clazz = TargetObject.class; | ||||||
|  |         Method method = clazz.getMethod("makeLowerCase", | ||||||
|  |                 new Class[] {String.class}); | ||||||
|  | 
 | ||||||
|  |         MethodDefinitionSourceAdvisor advisor = new MethodDefinitionSourceAdvisor(getInterceptor()); | ||||||
|  |         assertFalse(advisor.matches(method, clazz)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void testAdvisorReturnsTrueWhenMethodInvocationIsDefined() | ||||||
|  |         throws Exception { | ||||||
|  |         Class clazz = TargetObject.class; | ||||||
|  |         Method method = clazz.getMethod("countLength", | ||||||
|  |                 new Class[] {String.class}); | ||||||
|  | 
 | ||||||
|  |         MethodDefinitionSourceAdvisor advisor = new MethodDefinitionSourceAdvisor(getInterceptor()); | ||||||
|  |         assertTrue(advisor.matches(method, clazz)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void testDetectsImproperlyConfiguredAdvice() { | ||||||
|  |         MethodSecurityInterceptor msi = new MethodSecurityInterceptor(); | ||||||
|  | 
 | ||||||
|  |         try { | ||||||
|  |             new MethodDefinitionSourceAdvisor(msi); | ||||||
|  |             fail( | ||||||
|  |                 "Should have detected null ObjectDefinitionSource and thrown AopConfigException"); | ||||||
|  |         } catch (AopConfigException expected) { | ||||||
|  |             assertTrue(true); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private MethodSecurityInterceptor getInterceptor() { | ||||||
|  |         MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor(); | ||||||
|  |         editor.setAsText( | ||||||
|  |             "net.sf.acegisecurity.TargetObject.countLength=ROLE_NOT_USED"); | ||||||
|  | 
 | ||||||
|  |         MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue(); | ||||||
|  | 
 | ||||||
|  |         MethodSecurityInterceptor msi = new MethodSecurityInterceptor(); | ||||||
|  |         msi.setObjectDefinitionSource(map); | ||||||
|  | 
 | ||||||
|  |         return msi; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -75,20 +75,23 @@ | |||||||
| 	 | 	 | ||||||
| 	<bean id="bankService" class="sample.attributes.BankServiceImpl"/> | 	<bean id="bankService" class="sample.attributes.BankServiceImpl"/> | ||||||
| 	 | 	 | ||||||
| 	<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> | 	<!-- | ||||||
| 		<!-- names of the interceptors that will be applied by the proxy --> | 		This bean is a postprocessor that will automatically apply relevant advisors | ||||||
|         <property name="interceptorNames"> | 		to any bean in child factories. | ||||||
|             <list> | 	--> | ||||||
|                 <value>securityInterceptor</value> | 	<bean id="autoproxy"  | ||||||
|             </list> | 		class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"> | ||||||
|         </property> | 	</bean> | ||||||
| 
 | 
 | ||||||
| 		<!-- the bean names to automatically generate proxies for --> | 	<!-- | ||||||
|         <property name="beanNames"> | 		AOP advisor that will automatically wire the MethodSecurityInterceptor (above) | ||||||
|             <list> | 		into BankServiceImpl (above). The configuration attributes used are obtained | ||||||
|                 <idref local="bankService"/> | 		from the securityInterceptor.objectDefinitionSouce, which in the | ||||||
|             </list> | 		above configuration is a Commons Attributes-based source. | ||||||
|         </property> | 	--> | ||||||
|     </bean> | 	<bean id="methodSecurityAdvisor" | ||||||
|  | 		class="net.sf.acegisecurity.intercept.method.MethodDefinitionSourceAdvisor" | ||||||
|  | 		autowire="constructor" > | ||||||
|  | 	</bean> | ||||||
| 
 | 
 | ||||||
| </beans> | </beans> | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user