SEC-271: implemented auto creation of AuthenticationProcessingFilter and it's dependencies
This commit is contained in:
parent
23024b20ad
commit
207e8b932e
|
@ -76,5 +76,18 @@ public class AuthenticationMechanismBeanDefinitionParser extends AbstractBeanDef
|
||||||
}
|
}
|
||||||
return authMechanismBeanDef;
|
return authMechanismBeanDef;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Creates a default bean definition.
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
protected static RootBeanDefinition createBeanDefinitionWithDefaults() {
|
||||||
|
RootBeanDefinition authMechanismBeanDef = new RootBeanDefinition(ProviderManager.class);
|
||||||
|
ManagedList providers = new ManagedList();
|
||||||
|
// create authentication-repository (DaoAuthenticationProvider) and add that to list
|
||||||
|
RootBeanDefinition authRepo = AuthenticationRepositoryBeanDefinitionParser.createBeanDefinitionWithDefaults();
|
||||||
|
providers.add(authRepo);
|
||||||
|
authMechanismBeanDef.getPropertyValues().addPropertyValue("providers", providers);
|
||||||
|
return authMechanismBeanDef;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
*/
|
*/
|
||||||
package org.acegisecurity.config;
|
package org.acegisecurity.config;
|
||||||
|
|
||||||
|
import org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices;
|
||||||
import org.acegisecurity.ui.webapp.AuthenticationProcessingFilter;
|
import org.acegisecurity.ui.webapp.AuthenticationProcessingFilter;
|
||||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
|
@ -39,10 +40,10 @@ public class AuthenticationProcessingFilterBeanDefinitionParser extends Abstract
|
||||||
setPropertyIfAvailable(element, ERROR_FORM_URL, "authenticationFailureUrl", definition);
|
setPropertyIfAvailable(element, ERROR_FORM_URL, "authenticationFailureUrl", definition);
|
||||||
setPropertyIfAvailable(element, DEFAULT_TARGET_URL, "defaultTargetUrl", definition);
|
setPropertyIfAvailable(element, DEFAULT_TARGET_URL, "defaultTargetUrl", definition);
|
||||||
|
|
||||||
// register BFPP to re-unite all other collaborators
|
// register BFPP to wire all other collaborators
|
||||||
RootBeanDefinition postProcessor = new RootBeanDefinition(
|
// RootBeanDefinition postProcessor = new RootBeanDefinition(
|
||||||
AuthenticationProcessingFilterDependenciesConfigurer.class);
|
// AuthenticationProcessingFilterDependenciesConfigurer.class);
|
||||||
parserContext.getReaderContext().registerWithGeneratedName(postProcessor);
|
// parserContext.getReaderContext().registerWithGeneratedName(postProcessor);
|
||||||
|
|
||||||
return definition;
|
return definition;
|
||||||
}
|
}
|
||||||
|
@ -55,4 +56,16 @@ public class AuthenticationProcessingFilterBeanDefinitionParser extends Abstract
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static RootBeanDefinition createBeandefinitionWithDefaults() {
|
||||||
|
RootBeanDefinition definition = new RootBeanDefinition(AuthenticationProcessingFilter.class);
|
||||||
|
definition.getPropertyValues().addPropertyValue("authenticationManager",
|
||||||
|
AuthenticationMechanismBeanDefinitionParser.createBeanDefinitionWithDefaults());
|
||||||
|
definition.getPropertyValues().addPropertyValue("rememberMeServices",
|
||||||
|
RememberMeServicesBeanDefinitionParser.doCreateBeanDefintionWithDefaults());
|
||||||
|
/* TODO: There should not be any defaults for these urls ?!?! */
|
||||||
|
definition.getPropertyValues().addPropertyValue("authenticationFailureUrl", "/acegilogin.jsp?login_error=1");
|
||||||
|
definition.getPropertyValues().addPropertyValue("defaultTargetUrl", "/");
|
||||||
|
return definition;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import org.acegisecurity.providers.encoding.Md5PasswordEncoder;
|
||||||
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||||
|
import org.springframework.beans.factory.support.ManagedList;
|
||||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
|
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
|
||||||
import org.springframework.beans.factory.xml.ParserContext;
|
import org.springframework.beans.factory.xml.ParserContext;
|
||||||
|
@ -185,5 +186,10 @@ public class AuthenticationRepositoryBeanDefinitionParser extends AbstractBeanDe
|
||||||
saltSourceTypeElement.getAttribute("systemWideSalt"));
|
saltSourceTypeElement.getAttribute("systemWideSalt"));
|
||||||
return definition;
|
return definition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static RootBeanDefinition createBeanDefinitionWithDefaults() {
|
||||||
|
RootBeanDefinition repositoryBeanDef = new RootBeanDefinition(DaoAuthenticationProvider.class);
|
||||||
|
return repositoryBeanDef;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,10 +26,15 @@ public class AutoConfigBeanDefinitionParser implements BeanDefinitionParser {
|
||||||
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||||
createAndRegisterBeanDefinitionForHttpSessionContextIntegrationFilter(parserContext);
|
createAndRegisterBeanDefinitionForHttpSessionContextIntegrationFilter(parserContext);
|
||||||
createAndRegisterBeanDefinitionForLogoutFilter(parserContext);
|
createAndRegisterBeanDefinitionForLogoutFilter(parserContext);
|
||||||
|
createAndRegisterBeanDefinitionForAuthenticationProcessingFilter(parserContext);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void createAndRegisterBeanDefinitionForAuthenticationProcessingFilter(ParserContext parserContext) {
|
||||||
|
RootBeanDefinition defintion = AuthenticationProcessingFilterBeanDefinitionParser.createBeandefinitionWithDefaults();
|
||||||
|
registerBeanDefinition(parserContext, defintion);
|
||||||
|
}
|
||||||
|
|
||||||
private void createAndRegisterBeanDefinitionForLogoutFilter(ParserContext parserContext) {
|
private void createAndRegisterBeanDefinitionForLogoutFilter(ParserContext parserContext) {
|
||||||
RootBeanDefinition defintion =LogoutFilterBeanDefinitionParser.doCreateBeanDefinitionWithDefaults();
|
RootBeanDefinition defintion =LogoutFilterBeanDefinitionParser.doCreateBeanDefinitionWithDefaults();
|
||||||
registerBeanDefinition(parserContext, defintion);
|
registerBeanDefinition(parserContext, defintion);
|
||||||
|
|
|
@ -4,12 +4,16 @@
|
||||||
package org.acegisecurity.config;
|
package org.acegisecurity.config;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import org.acegisecurity.AuthenticationManager;
|
||||||
import org.acegisecurity.context.HttpSessionContextIntegrationFilter;
|
import org.acegisecurity.context.HttpSessionContextIntegrationFilter;
|
||||||
import org.acegisecurity.ui.logout.LogoutFilter;
|
import org.acegisecurity.ui.logout.LogoutFilter;
|
||||||
import org.acegisecurity.ui.logout.LogoutHandler;
|
import org.acegisecurity.ui.logout.LogoutHandler;
|
||||||
|
import org.acegisecurity.ui.rememberme.RememberMeServices;
|
||||||
|
import org.acegisecurity.ui.webapp.AuthenticationProcessingFilter;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
@ -43,21 +47,34 @@ public class AutoConfigBeanDefinitionParserTests extends TestCase {
|
||||||
assertFalse(filter.isForceEagerSessionCreation());
|
assertFalse(filter.isForceEagerSessionCreation());
|
||||||
assertFalse(filter.isCloneFromHttpSession());
|
assertFalse(filter.isCloneFromHttpSession());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testLogoutFilterDefinitionCreatedWithDefaults() throws Exception{
|
public void testLogoutFilterDefinitionCreatedWithDefaults() throws Exception {
|
||||||
String[] names = bf.getBeanNamesForType(LogoutFilter.class);
|
String[] names = bf.getBeanNamesForType(LogoutFilter.class);
|
||||||
assertEquals(1, names.length);
|
assertEquals(1, names.length);
|
||||||
LogoutFilter filter = (LogoutFilter) context.getBean(names[0]);
|
LogoutFilter filter = (LogoutFilter) context.getBean(names[0]);
|
||||||
assertNotNull(filter);
|
assertNotNull(filter);
|
||||||
Field logoutSuccessUrl = makeAccessibleAndGetFieldByName(filter.getClass().getDeclaredFields(), "logoutSuccessUrl");
|
Field logoutSuccessUrl = makeAccessibleAndGetFieldByName(filter.getClass().getDeclaredFields(),
|
||||||
|
"logoutSuccessUrl");
|
||||||
String value = (String) logoutSuccessUrl.get(filter);
|
String value = (String) logoutSuccessUrl.get(filter);
|
||||||
assertEquals("/", value);
|
assertEquals("/", value);
|
||||||
Field handlers = makeAccessibleAndGetFieldByName(filter.getClass().getDeclaredFields(), "handlers");
|
Field handlers = makeAccessibleAndGetFieldByName(filter.getClass().getDeclaredFields(), "handlers");
|
||||||
assertNotNull(handlers);
|
assertNotNull(handlers);
|
||||||
LogoutHandler[] handlersArray = (LogoutHandler[])handlers.get(filter);
|
LogoutHandler[] handlersArray = (LogoutHandler[]) handlers.get(filter);
|
||||||
assertEquals(2, handlersArray.length);
|
assertEquals(2, handlersArray.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testExceptionTranslationFilterCreatedwithDefaults() throws Exception {
|
||||||
|
Map map = bf.getBeansOfType(AuthenticationProcessingFilter.class);
|
||||||
|
AuthenticationProcessingFilter filter = (AuthenticationProcessingFilter) map.values().iterator().next();
|
||||||
|
AuthenticationManager authMgr = filter.getAuthenticationManager();
|
||||||
|
assertNotNull(authMgr);
|
||||||
|
RememberMeServices remMeServices = filter.getRememberMeServices();
|
||||||
|
assertNotNull(remMeServices);
|
||||||
|
assertEquals("/acegilogin.jsp?login_error=1",filter.getAuthenticationFailureUrl());
|
||||||
|
assertEquals( "/",filter.getDefaultTargetUrl());
|
||||||
|
}
|
||||||
|
|
||||||
private Field makeAccessibleAndGetFieldByName(Field[] declaredFields, String name) {
|
private Field makeAccessibleAndGetFieldByName(Field[] declaredFields, String name) {
|
||||||
Field field = null;
|
Field field = null;
|
||||||
for (int i = 0, n = declaredFields.length; i < n; i++) {
|
for (int i = 0, n = declaredFields.length; i < n; i++) {
|
||||||
|
|
Loading…
Reference in New Issue