Browse Source
http://jira.springframework.org/browse/SEC-783. Added support for custom-after-invocation-provider2.0.x
9 changed files with 177 additions and 11 deletions
@ -0,0 +1,24 @@ |
|||||||
|
package org.springframework.security.config; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.BeanDefinitionHolder; |
||||||
|
import org.springframework.beans.factory.xml.BeanDefinitionDecorator; |
||||||
|
import org.springframework.beans.factory.xml.ParserContext; |
||||||
|
import org.w3c.dom.Node; |
||||||
|
|
||||||
|
/** |
||||||
|
* Adds the decorated {@link org.springframework.security.afterinvocation.AfterInvocationProvider} to the |
||||||
|
* AfterInvocationProviderManager's list. |
||||||
|
* |
||||||
|
* @author Luke Taylor |
||||||
|
* @version $Id$ |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public class CustomAfterInvocationProviderBeanDefinitionDecorator implements BeanDefinitionDecorator { |
||||||
|
|
||||||
|
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder holder, ParserContext parserContext) { |
||||||
|
ConfigUtils.getRegisteredAfterInvocationProviders(parserContext).add(holder.getBeanDefinition()); |
||||||
|
|
||||||
|
return holder; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,48 @@ |
|||||||
|
package org.springframework.security.config; |
||||||
|
|
||||||
|
import org.apache.commons.logging.Log; |
||||||
|
import org.apache.commons.logging.LogFactory; |
||||||
|
import org.springframework.beans.BeansException; |
||||||
|
import org.springframework.beans.factory.BeanFactory; |
||||||
|
import org.springframework.beans.factory.BeanFactoryAware; |
||||||
|
import org.springframework.beans.factory.config.BeanPostProcessor; |
||||||
|
import org.springframework.security.AfterInvocationManager; |
||||||
|
import org.springframework.security.intercept.method.aopalliance.MethodSecurityInterceptor; |
||||||
|
|
||||||
|
/** |
||||||
|
* BeanPostProcessor which sets the AfterInvocationManager on the default MethodSecurityInterceptor, |
||||||
|
* if one has been configured. |
||||||
|
* |
||||||
|
* @author Luke Taylor |
||||||
|
* @version $Id$ |
||||||
|
* |
||||||
|
*/ |
||||||
|
public class MethodSecurityInterceptorPostProcessor implements BeanPostProcessor, BeanFactoryAware{ |
||||||
|
private Log logger = LogFactory.getLog(getClass()); |
||||||
|
|
||||||
|
private BeanFactory beanFactory; |
||||||
|
|
||||||
|
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
||||||
|
if(!beanName.equals(BeanIds.METHOD_SECURITY_INTERCEPTOR)) { |
||||||
|
return bean; |
||||||
|
} |
||||||
|
|
||||||
|
MethodSecurityInterceptor interceptor = (MethodSecurityInterceptor) bean; |
||||||
|
|
||||||
|
if (beanFactory.containsBean(BeanIds.AFTER_INVOCATION_MANAGER)) { |
||||||
|
logger.debug("Setting AfterInvocationManaer on MethodSecurityInterceptor"); |
||||||
|
interceptor.setAfterInvocationManager((AfterInvocationManager) |
||||||
|
beanFactory.getBean(BeanIds.AFTER_INVOCATION_MANAGER)); |
||||||
|
} |
||||||
|
|
||||||
|
return bean; |
||||||
|
} |
||||||
|
|
||||||
|
public Object postProcessAfterInitialization(Object bean, String beanName) { |
||||||
|
return bean; |
||||||
|
} |
||||||
|
|
||||||
|
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
||||||
|
this.beanFactory = beanFactory; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,43 @@ |
|||||||
|
package org.springframework.security.config; |
||||||
|
|
||||||
|
import static org.junit.Assert.*; |
||||||
|
|
||||||
|
import org.junit.After; |
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.context.support.AbstractXmlApplicationContext; |
||||||
|
import org.springframework.security.afterinvocation.AfterInvocationProviderManager; |
||||||
|
import org.springframework.security.intercept.method.aopalliance.MethodSecurityInterceptor; |
||||||
|
import org.springframework.security.util.InMemoryXmlApplicationContext; |
||||||
|
|
||||||
|
public class CustomAfterInvocationProviderBeanDefinitionDecoratorTests { |
||||||
|
private AbstractXmlApplicationContext appContext; |
||||||
|
|
||||||
|
@After |
||||||
|
public void closeAppContext() { |
||||||
|
if (appContext != null) { |
||||||
|
appContext.close(); |
||||||
|
appContext = null; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void customAuthenticationProviderIsAddedToInterceptor() { |
||||||
|
setContext( |
||||||
|
"<global-method-security />" + |
||||||
|
"<b:bean id='aip' class='org.springframework.security.config.MockAfterInvocationProvider'>" + |
||||||
|
" <custom-after-invocation-provider />" + |
||||||
|
"</b:bean>" + |
||||||
|
HttpSecurityBeanDefinitionParserTests.AUTH_PROVIDER_XML |
||||||
|
); |
||||||
|
|
||||||
|
MethodSecurityInterceptor msi = (MethodSecurityInterceptor) appContext.getBean(BeanIds.METHOD_SECURITY_INTERCEPTOR); |
||||||
|
AfterInvocationProviderManager apm = (AfterInvocationProviderManager) msi.getAfterInvocationManager(); |
||||||
|
assertNotNull(apm); |
||||||
|
assertEquals(1, apm.getProviders().size()); |
||||||
|
assertTrue(apm.getProviders().get(0) instanceof MockAfterInvocationProvider); |
||||||
|
} |
||||||
|
|
||||||
|
private void setContext(String context) { |
||||||
|
appContext = new InMemoryXmlApplicationContext(context); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,24 @@ |
|||||||
|
package org.springframework.security.config; |
||||||
|
|
||||||
|
import org.springframework.security.AccessDeniedException; |
||||||
|
import org.springframework.security.Authentication; |
||||||
|
import org.springframework.security.ConfigAttribute; |
||||||
|
import org.springframework.security.ConfigAttributeDefinition; |
||||||
|
import org.springframework.security.afterinvocation.AfterInvocationProvider; |
||||||
|
|
||||||
|
public class MockAfterInvocationProvider implements AfterInvocationProvider { |
||||||
|
|
||||||
|
public Object decide(Authentication authentication, Object object, ConfigAttributeDefinition config, Object returnedObject) |
||||||
|
throws AccessDeniedException { |
||||||
|
return returnedObject; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean supports(ConfigAttribute attribute) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean supports(Class clazz) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue