SEC-1034: Removed classes for converting a FilterInvocationDefinitionSource to a map for use in FilterChainProxy

This commit is contained in:
Luke Taylor 2008-11-15 10:26:35 +00:00
parent 31375b7212
commit e259fe43a9
4 changed files with 24 additions and 123 deletions

View File

@ -237,14 +237,6 @@ public class DefaultFilterInvocationDefinitionSource implements FilterInvocation
return FilterInvocation.class.isAssignableFrom(clazz);
}
public int getMapSize() {
return this.requestMap.size();
}
/*Map<Object, List<ConfigAttribute>>*/ Map getRequestMap() {
return requestMap;
}
protected UrlMatcher getUrlMatcher() {
return urlMatcher;
}

View File

@ -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<String, List<Filter>> filterChainMap = new LinkedHashMap<String, List<Filter>>();
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<Object, List<ConfigAttribute>> requestMap = fids.getRequestMap();
for(Object entry : requestMap.keySet()) {
String path = entry instanceof Pattern ? ((Pattern)entry).pattern() : (String)entry;
List<ConfigAttribute> configAttributeDefinition = requestMap.get(entry);
List<Filter> filters = new ArrayList<Filter>();
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<String, List<Filter>> getFilterChainMap() {
return filterChainMap;
}
public UrlMatcher getMatcher() {
return matcher;
}
}

View File

@ -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 <code>Filter</code> 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<String, List<Filter>> uncompiledFilterChainMap;
/** Compiled pattern version of the filter chain map */
private Map<Object, List<Filter>> 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.
*

View File

@ -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<Filter> filters = filterChainProxy.getFilters("/foo/blah");
assertEquals(1, filters.size());
assertTrue(filters.get(0) instanceof MockFilter);