From e259fe43a97bd48827cd6aa8c518f8746bc91182 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Sat, 15 Nov 2008 10:26:35 +0000 Subject: [PATCH] SEC-1034: Removed classes for converting a FilterInvocationDefinitionSource to a map for use in FilterChainProxy --- ...faultFilterInvocationDefinitionSource.java | 8 --- .../web/FIDSToFilterChainMapConverter.java | 63 ------------------- .../security/util/FilterChainProxy.java | 56 ++++++----------- .../security/util/FilterChainProxyTests.java | 20 ++---- 4 files changed, 24 insertions(+), 123 deletions(-) delete mode 100644 core/src/main/java/org/springframework/security/intercept/web/FIDSToFilterChainMapConverter.java diff --git a/core/src/main/java/org/springframework/security/intercept/web/DefaultFilterInvocationDefinitionSource.java b/core/src/main/java/org/springframework/security/intercept/web/DefaultFilterInvocationDefinitionSource.java index 30d43e3903..af1dd57ce0 100644 --- a/core/src/main/java/org/springframework/security/intercept/web/DefaultFilterInvocationDefinitionSource.java +++ b/core/src/main/java/org/springframework/security/intercept/web/DefaultFilterInvocationDefinitionSource.java @@ -237,14 +237,6 @@ public class DefaultFilterInvocationDefinitionSource implements FilterInvocation return FilterInvocation.class.isAssignableFrom(clazz); } - public int getMapSize() { - return this.requestMap.size(); - } - - /*Map>*/ Map getRequestMap() { - return requestMap; - } - protected UrlMatcher getUrlMatcher() { return urlMatcher; } diff --git a/core/src/main/java/org/springframework/security/intercept/web/FIDSToFilterChainMapConverter.java b/core/src/main/java/org/springframework/security/intercept/web/FIDSToFilterChainMapConverter.java deleted file mode 100644 index 311b52d675..0000000000 --- a/core/src/main/java/org/springframework/security/intercept/web/FIDSToFilterChainMapConverter.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.springframework.security.intercept.web; - -import org.springframework.context.ApplicationContext; -import org.springframework.util.Assert; -import org.springframework.security.ConfigAttribute; -import org.springframework.security.util.FilterChainProxy; -import org.springframework.security.util.UrlMatcher; - -import javax.servlet.Filter; -import java.util.*; -import java.util.regex.Pattern; - -/** - * Used internally to provide backward compatibility for configuration of FilterChainProxy using a - * FilterInvocationDefinitionSource. This is deprecated in favour of namespace-based configuration. - * - * This class will convert a FilterInvocationDefinitionSource into a suitable Map, provided it is one of the - * recognised implementations (ant path or regular expression). The order of the mappings will be - * preserved in the Map. - * - * @author Luke Taylor - * @version $Id$ - */ -public class FIDSToFilterChainMapConverter { - - private LinkedHashMap> filterChainMap = new LinkedHashMap>(); - private UrlMatcher matcher; - - public FIDSToFilterChainMapConverter(DefaultFilterInvocationDefinitionSource fids, ApplicationContext appContext) { - // TODO: Check if this is necessary. Retained from refactoring of FilterChainProxy - Assert.notNull(fids.getAllConfigAttributes(), "FilterChainProxy requires the " + - "FilterInvocationDefinitionSource to return a non-null response to getAllConfigAttributes()"); - matcher = fids.getUrlMatcher(); - Map> requestMap = fids.getRequestMap(); - - for(Object entry : requestMap.keySet()) { - String path = entry instanceof Pattern ? ((Pattern)entry).pattern() : (String)entry; - List configAttributeDefinition = requestMap.get(entry); - List filters = new ArrayList(); - - for(ConfigAttribute attr : configAttributeDefinition) { - String filterName = attr.getAttribute(); - - Assert.notNull(filterName, "Configuration attribute: '" + attr + "' returned null to the getAttribute() " + - "method, which is invalid when used with FilterChainProxy"); - - if (!filterName.equals(FilterChainProxy.TOKEN_NONE)) { - filters.add((Filter) appContext.getBean(filterName, Filter.class)); - } - } - - filterChainMap.put(path, filters); - } - } - - public Map> getFilterChainMap() { - return filterChainMap; - } - - public UrlMatcher getMatcher() { - return matcher; - } -} diff --git a/core/src/main/java/org/springframework/security/util/FilterChainProxy.java b/core/src/main/java/org/springframework/security/util/FilterChainProxy.java index 2cf6e642c5..847dd38749 100644 --- a/core/src/main/java/org/springframework/security/util/FilterChainProxy.java +++ b/core/src/main/java/org/springframework/security/util/FilterChainProxy.java @@ -15,21 +15,30 @@ package org.springframework.security.util; +import java.io.IOException; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.beans.BeansException; import org.springframework.beans.factory.InitializingBean; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.security.intercept.web.*; +import org.springframework.security.intercept.web.FilterInvocation; +import org.springframework.security.intercept.web.FilterInvocationDefinitionSource; import org.springframework.util.Assert; import org.springframework.web.filter.DelegatingFilterProxy; -import javax.servlet.*; - -import java.io.IOException; -import java.util.*; - /** * Delegates Filter requests to a list of Spring-managed beans. @@ -94,7 +103,7 @@ import java.util.*; * * @version $Id$ */ -public class FilterChainProxy implements Filter, InitializingBean, ApplicationContextAware { +public class FilterChainProxy implements Filter, InitializingBean { //~ Static fields/initializers ===================================================================================== private static final Log logger = LogFactory.getLog(FilterChainProxy.class); @@ -102,29 +111,18 @@ public class FilterChainProxy implements Filter, InitializingBean, ApplicationCo //~ Instance fields ================================================================================================ - private ApplicationContext applicationContext; +// private ApplicationContext applicationContext; /** Map of the original pattern Strings to filter chains */ private Map> uncompiledFilterChainMap; /** Compiled pattern version of the filter chain map */ private Map> filterChainMap; private UrlMatcher matcher = new AntUrlPathMatcher(); private boolean stripQueryStringFromUrls = true; - private DefaultFilterInvocationDefinitionSource fids; //~ Methods ======================================================================================================== public void afterPropertiesSet() throws Exception { - // Convert the FilterDefinitionSource to a filterChainMap if set - if (fids != null) { - Assert.isNull(uncompiledFilterChainMap, "Set the filterChainMap or FilterInvocationDefinitionSource but not both"); - FIDSToFilterChainMapConverter converter = new FIDSToFilterChainMapConverter(fids, applicationContext); - setMatcher(converter.getMatcher()); - setFilterChainMap(converter.getFilterChainMap()); - fids = null; - } - Assert.notNull(uncompiledFilterChainMap, "filterChainMap must be set"); - } public void init(FilterConfig filterConfig) throws ServletException { @@ -233,20 +231,6 @@ public class FilterChainProxy implements Filter, InitializingBean, ApplicationCo return allFilters; } - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.applicationContext = applicationContext; - } - - /** - * - * @deprecated Use namespace configuration or call setFilterChainMap instead. - */ - public void setFilterInvocationDefinitionSource(FilterInvocationDefinitionSource fids) { - Assert.isInstanceOf(DefaultFilterInvocationDefinitionSource.class, fids, - "Must be a DefaultFilterInvocationDefinitionSource"); - this.fids = (DefaultFilterInvocationDefinitionSource) fids; - } - /** * Sets the mapping of URL patterns to filter chains. * diff --git a/core/src/test/java/org/springframework/security/util/FilterChainProxyTests.java b/core/src/test/java/org/springframework/security/util/FilterChainProxyTests.java index 0f762c796f..be9ffe01dd 100644 --- a/core/src/test/java/org/springframework/security/util/FilterChainProxyTests.java +++ b/core/src/test/java/org/springframework/security/util/FilterChainProxyTests.java @@ -15,21 +15,17 @@ package org.springframework.security.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; import java.util.List; +import javax.servlet.Filter; + import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.context.support.StaticApplicationContext; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.security.MockFilterConfig; @@ -60,14 +56,6 @@ public class FilterChainProxyTests { } } - @Test(expected = IllegalArgumentException.class) - public void testDetectsMissingFilterInvocationDefinitionSource() throws Exception { - FilterChainProxy filterChainProxy = new FilterChainProxy(); - filterChainProxy.setApplicationContext(new StaticApplicationContext()); - - filterChainProxy.afterPropertiesSet(); - } - @Test public void testDoNotFilter() throws Exception { FilterChainProxy filterChainProxy = (FilterChainProxy) appCtx.getBean("filterChain", FilterChainProxy.class); @@ -136,7 +124,7 @@ public class FilterChainProxyTests { } private void checkPathAndFilterOrder(FilterChainProxy filterChainProxy) throws Exception { - List filters = filterChainProxy.getFilters("/foo/blah"); + List filters = filterChainProxy.getFilters("/foo/blah"); assertEquals(1, filters.size()); assertTrue(filters.get(0) instanceof MockFilter);