org.springframework.security.web
Class FilterChainProxy

java.lang.Object
  extended by org.springframework.web.filter.GenericFilterBean
      extended by org.springframework.security.web.FilterChainProxy
All Implemented Interfaces:
Filter, BeanNameAware, DisposableBean, InitializingBean, ServletContextAware

public class FilterChainProxy
extends GenericFilterBean

Delegates Filter requests to a list of Spring-managed beans. As of version 2.0, you shouldn't need to explicitly configure a FilterChainProxy bean in your application context unless you need very fine control over the filter chain contents. Most cases should be adequately covered by the default <security:http /> namespace configuration options.

The FilterChainProxy is loaded via a standard Spring DelegatingFilterProxy declaration in web.xml. FilterChainProxy will then pass GenericFilterBean.init(FilterConfig), GenericFilterBean.destroy() and doFilter(ServletRequest, ServletResponse, FilterChain) invocations through to each Filter defined against FilterChainProxy.

As of version 2.0, FilterChainProxy is configured using an ordered Map of path patterns to Lists of Filter objects. In previous versions, a FilterInvocationSecurityMetadataSource was used. This is now deprecated in favour of namespace-based configuration which provides a more robust and simplfied syntax. The Map instance will normally be created while parsing the namespace configuration, so doesn't have to be set explicitly. Instead the <filter-chain-map> element should be used within the FilterChainProxy bean declaration. This in turn should have a list of child <filter-chain> elements which each define a URI pattern and the list of filters (as comma-separated bean names) which should be applied to requests which match the pattern. An example configuration might look like this:

 <bean id="myfilterChainProxy" class="org.springframework.security.util.FilterChainProxy">
     <security:filter-chain-map pathType="ant">
         <security:filter-chain pattern="/do/not/filter" filters="none"/>
         <security:filter-chain pattern="/**" filters="filter1,filter2,filter3"/>
     </security:filter-chain-map>
 </bean>
 
The names "filter1", "filter2", "filter3" should be the bean names of Filter instances defined in the application context. The order of the names defines the order in which the filters will be applied. As shown above, use of the value "none" for the "filters" can be used to exclude Please consult the security namespace schema file for a full list of available configuration options.

Each possible URI pattern that FilterChainProxy should service must be entered. The first matching URI pattern for a given request will be used to define all of the Filters that apply to that request. NB: This means you must put most specific URI patterns at the top of the list, and ensure all Filters that should apply for a given URI pattern are entered against the respective entry. The FilterChainProxy will not iterate the remainder of the URI patterns to locate additional Filters.

FilterChainProxy respects normal handling of Filters that elect not to call Filter.doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain), in that the remainder of the original or FilterChainProxy-declared filter chain will not be called.

Note the Filter lifecycle mismatch between the servlet container and IoC container. As described in the DelegatingFilterProxy JavaDocs, we recommend you allow the IoC container to manage the lifecycle instead of the servlet container. By default the DelegatingFilterProxy will never call this class' GenericFilterBean.init(FilterConfig) and GenericFilterBean.destroy() methods, which in turns means that the corresponding methods on the filter beans managed by this class will never be called. If you do need your filters to be initialized and destroyed, please set the targetFilterLifecycle initialization parameter against the DelegatingFilterProxy to specify that servlet container lifecycle management should be used. You don't need to worry about this in most cases.

Version:
$Id: FilterChainProxy.java 4075 2009-12-18 18:44:42Z ltaylor $
Author:
Carlos Sanchez, Ben Alex, Luke Taylor

Nested Class Summary
static interface FilterChainProxy.FilterChainValidator
           
 
Field Summary
static String TOKEN_NONE
           
 
Constructor Summary
FilterChainProxy()
           
 
Method Summary
 void afterPropertiesSet()
           
 void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
           
 Map<String,List<Filter>> getFilterChainMap()
          Returns a copy of the underlying filter chain map.
 List<Filter> getFilters(String url)
          Returns the first filter chain matching the supplied URL.
 UrlMatcher getMatcher()
           
protected  Collection<Filter> obtainAllDefinedFilters()
          Obtains all of the uniqueFilter instances registered in the map of filter chains.
 void setFilterChainMap(Map filterChainMap)
          Sets the mapping of URL patterns to filter chains.
 void setFilterChainValidator(FilterChainProxy.FilterChainValidator filterChainValidator)
          Used (internally) to specify a validation strategy for the filters in each configured chain.
 void setMatcher(UrlMatcher matcher)
           
 void setStripQueryStringFromUrls(boolean stripQueryStringFromUrls)
          If set to 'true', the query string will be stripped from the request URL before attempting to find a matching filter chain.
 String toString()
           
 
Methods inherited from class org.springframework.web.filter.GenericFilterBean
addRequiredProperty, destroy, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setServletContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TOKEN_NONE

public static final String TOKEN_NONE
See Also:
Constant Field Values
Constructor Detail

FilterChainProxy

public FilterChainProxy()
Method Detail

afterPropertiesSet

public void afterPropertiesSet()
Specified by:
afterPropertiesSet in interface InitializingBean
Overrides:
afterPropertiesSet in class GenericFilterBean

doFilter

public void doFilter(ServletRequest request,
                     ServletResponse response,
                     FilterChain chain)
              throws IOException,
                     ServletException
Throws:
IOException
ServletException

getFilters

public List<Filter> getFilters(String url)
Returns the first filter chain matching the supplied URL.

Parameters:
url - the request URL
Returns:
an ordered array of Filters defining the filter chain

obtainAllDefinedFilters

protected Collection<Filter> obtainAllDefinedFilters()
Obtains all of the uniqueFilter instances registered in the map of filter chains.

This is useful in ensuring a Filter is not initialized or destroyed twice.

Returns:
all of the Filter instances in the application context which have an entry in the map (only one entry is included in the array for each Filter that actually exists in application context, even if a given Filter is defined multiples times in the filter chain map)

setFilterChainMap

public void setFilterChainMap(Map filterChainMap)
Sets the mapping of URL patterns to filter chains. The map keys should be the paths and the values should be arrays of Filter objects. It's VERY important that the type of map used preserves ordering - the order in which the iterator returns the entries must be the same as the order they were added to the map, otherwise you have no way of guaranteeing that the most specific patterns are returned before the more general ones. So make sure the Map used is an instance of LinkedHashMap or an equivalent, rather than a plain HashMap, for example.

Parameters:
filterChainMap - the map of path Strings to List<Filter>s.

getFilterChainMap

public Map<String,List<Filter>> getFilterChainMap()
Returns a copy of the underlying filter chain map. Modifications to the map contents will not affect the FilterChainProxy state - to change the map call setFilterChainMap.

Returns:
the map of path pattern Strings to filter chain lists (with ordering guaranteed).

setMatcher

public void setMatcher(UrlMatcher matcher)

getMatcher

public UrlMatcher getMatcher()

setStripQueryStringFromUrls

public void setStripQueryStringFromUrls(boolean stripQueryStringFromUrls)
If set to 'true', the query string will be stripped from the request URL before attempting to find a matching filter chain. This is the default value.


setFilterChainValidator

public void setFilterChainValidator(FilterChainProxy.FilterChainValidator filterChainValidator)
Used (internally) to specify a validation strategy for the filters in each configured chain.

Parameters:
filterChainValidator -

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2004-2009 SpringSource, Inc. All Rights Reserved.