Class DefaultFilterChainManager

java.lang.Object
org.apache.shiro.web.filter.mgt.DefaultFilterChainManager
All Implemented Interfaces:
FilterChainManager

public class DefaultFilterChainManager extends Object implements FilterChainManager
Default FilterChainManager implementation maintaining a map of Filter instances (key: filter name, value: Filter) as well as a map of NamedFilterLists created from these Filters (key: filter chain name, value: NamedFilterList). The NamedFilterList is essentially a FilterChain that also has a name property by which it can be looked up.
Since:
1.0
See Also:
  • Constructor Details

  • Method Details

    • getFilterConfig

      public javax.servlet.FilterConfig getFilterConfig()
      Returns the FilterConfig provided by the Servlet container at webapp startup.
      Returns:
      the FilterConfig provided by the Servlet container at webapp startup.
    • setFilterConfig

      public void setFilterConfig(javax.servlet.FilterConfig filterConfig)
      Sets the FilterConfig provided by the Servlet container at webapp startup.
      Parameters:
      filterConfig - the FilterConfig provided by the Servlet container at webapp startup.
    • getFilters

      public Map<String,javax.servlet.Filter> getFilters()
      Description copied from interface: FilterChainManager
      Returns the pool of available Filters managed by this manager, keyed by name.
      Specified by:
      getFilters in interface FilterChainManager
      Returns:
      the pool of available Filters managed by this manager, keyed by name.
    • setFilters

      public void setFilters(Map<String,javax.servlet.Filter> filters)
    • getFilterChains

    • setFilterChains

      public void setFilterChains(Map<String,NamedFilterList> filterChains)
    • getFilter

      public javax.servlet.Filter getFilter(String name)
    • addFilter

      public void addFilter(String name, javax.servlet.Filter filter)
      Description copied from interface: FilterChainManager
      Adds a filter to the 'pool' of available filters that can be used when creating filter chains.

      Calling this method is effectively the same as calling addFilter(name, filter, false);

      Specified by:
      addFilter in interface FilterChainManager
      Parameters:
      name - the name to assign to the filter, used to reference the filter in chain definitions
      filter - the filter to initialize and then add to the pool of available filters that can be used
    • addFilter

      public void addFilter(String name, javax.servlet.Filter filter, boolean init)
      Description copied from interface: FilterChainManager
      Adds a filter to the 'pool' of available filters that can be used when creating filter chains.
      Specified by:
      addFilter in interface FilterChainManager
      Parameters:
      name - the name to assign to the filter, used to reference the filter in chain definitions
      filter - the filter to assign to the filter pool
      init - whether or not the Filter should be initialized first before being added to the pool.
    • createDefaultChain

      public void createDefaultChain(String chainName)
      Description copied from interface: FilterChainManager
      Creates a chain that should match any non-matched request paths, typically /** assuming an AntPathMatcher I used.
      Specified by:
      createDefaultChain in interface FilterChainManager
      Parameters:
      chainName - The name of the chain to create, likely /**.
      See Also:
      • AntPathMatcher
    • createChain

      public void createChain(String chainName, String chainDefinition)
      Description copied from interface: FilterChainManager
      Creates a filter chain for the given chainName with the specified chainDefinition String.

      Conventional Use

      Because the FilterChainManager interface does not impose any restrictions on filter chain names, (it expects only Strings), a convenient convention is to make the chain name an actual URL path expression (such as an Ant path expression). For example:

      createChain(path_expression, path_specific_filter_chain_definition); This convention can be used by a FilterChainResolver to inspect request URL paths against the chain name (path) and, if a match is found, return the corresponding chain for runtime filtering.

      Chain Definition Format

      The chainDefinition method argument is expected to conform to the following format:
       filter1[optional_config1], filter2[optional_config2], ..., filterN[optional_configN]
      where
      1. filterN is the name of a filter previously registered with the manager, and
      2. [optional_configN] is an optional bracketed string that has meaning for that particular filter for this particular chain
      If the filter does not need specific config for that chain name/URL path, you may discard the brackets - that is, filterN[] just becomes filterN.

      And because this method does create a chain, remember that order matters! The comma-delimited filter tokens in the chainDefinition specify the chain's execution order.

      Examples

      /account/** = authcBasic
      This example says "Create a filter named '/account/**' consisting of only the 'authcBasic' filter". Also because the authcBasic filter does not need any path-specific config, it doesn't have any config brackets [].

      /remoting/** = authcBasic, roles[b2bClient], perms["remote:invoke:wan,lan"]
      This example by contrast uses the 'roles' and 'perms' filters which do use bracket notation. This definition says:

      Construct a filter chain named '/remoting/**' which

      1. ensures the user is first authenticated (authcBasic) then
      2. ensures that user has the b2bClient role, and then finally
      3. ensures that they have the remote:invoke:lan,wan permission.

      Note: because elements within brackets [ ] can be comma-delimited themselves, you must quote the internal bracket definition if commas are needed (the above example has 'lan,wan'). If we didn't do that, the parser would interpret the chain definition as four tokens:

      1. authcBasic
      2. roles[b2bclient]
      3. perms[remote:invoke:lan
      4. wan]
      which is obviously incorrect. So remember to use quotes if your internal bracket definitions need to use commas.
      Specified by:
      createChain in interface FilterChainManager
      Parameters:
      chainName - the name to associate with the chain, conventionally a URL path pattern.
      chainDefinition - the string-formatted chain definition used to construct an actual NamedFilterList chain instance.
      See Also:
    • splitChainDefinition

      protected String[] splitChainDefinition(String chainDefinition)
      Splits the comma-delimited filter chain definition line into individual filter definition tokens.

      Example Input:

           foo, bar[baz], blah[x, y]
       
      Resulting Output:
           output[0] == foo
           output[1] == bar[baz]
           output[2] == blah[x, y]
       
      Parameters:
      chainDefinition - the comma-delimited filter chain definition.
      Returns:
      an array of filter definition tokens
      Since:
      1.2
      See Also:
    • toNameConfigPair

      protected String[] toNameConfigPair(String token) throws org.apache.shiro.config.ConfigurationException
      Based on the given filter chain definition token (e.g. 'foo' or 'foo[bar, baz]'), this will return the token as a name/value pair, removing any brackets as necessary. Examples:
      Input Result
      foo returned[0] == foo
      returned[1] == null
      foo[bar, baz] returned[0] == foo
      returned[1] == bar, baz
      Parameters:
      token - the filter chain definition token
      Returns:
      A name/value pair representing the filter name and a (possibly null) config value.
      Throws:
      org.apache.shiro.config.ConfigurationException - if the token cannot be parsed
      Since:
      1.2
      See Also:
    • addFilter

      protected void addFilter(String name, javax.servlet.Filter filter, boolean init, boolean overwrite)
    • addToChain

      public void addToChain(String chainName, String filterName)
      Description copied from interface: FilterChainManager
      Adds (appends) a filter to the filter chain identified by the given chainName. If there is no chain with the given name, a new one is created and the filter will be the first in the chain.
      Specified by:
      addToChain in interface FilterChainManager
      Parameters:
      chainName - the name of the chain where the filter will be appended.
      filterName - the name of the registered filter to add to the chain.
    • addToChain

      public void addToChain(String chainName, String filterName, String chainSpecificFilterConfig)
      Description copied from interface: FilterChainManager
      Adds (appends) a filter to the filter chain identified by the given chainName. If there is no chain with the given name, a new one is created and the filter will be the first in the chain.

      Note that the final argument expects the associated filter to be an instance of a PathConfigProcessor to accept per-chain configuration. If it is not, a IllegalArgumentException will be thrown.

      Specified by:
      addToChain in interface FilterChainManager
      Parameters:
      chainName - the name of the chain where the filter will be appended.
      filterName - the name of the registered filter to add to the chain.
      chainSpecificFilterConfig - the filter-specific configuration that should be applied for only the specified filter chain.
    • setGlobalFilters

      public void setGlobalFilters(List<String> globalFilterNames) throws org.apache.shiro.config.ConfigurationException
      Description copied from interface: FilterChainManager
      Configures the set of named filters that will match all paths. These filters will match BEFORE explicitly configured filter chains i.e. by calling FilterChainManager.createChain(String, String), FilterChainManager.addToChain(String, String), etc.
      Filters configured in this list wll apply to ALL requests.
      Specified by:
      setGlobalFilters in interface FilterChainManager
      Parameters:
      globalFilterNames - the list of filter names to match ALL paths.
      Throws:
      org.apache.shiro.config.ConfigurationException - if one of the filter names is invalid and cannot be loaded from the set of configured filters FilterChainManager.getFilters()}.
    • applyChainConfig

      protected void applyChainConfig(String chainName, javax.servlet.Filter filter, String chainSpecificFilterConfig)
    • ensureChain

      protected NamedFilterList ensureChain(String chainName)
    • getChain

      public NamedFilterList getChain(String chainName)
      Description copied from interface: FilterChainManager
      Returns the filter chain identified by the specified chainName or null if there is no chain with that name.
      Specified by:
      getChain in interface FilterChainManager
      Parameters:
      chainName - the name identifying the filter chain.
      Returns:
      the filter chain identified by the specified chainName or null if there is no chain with that name.
    • hasChains

      public boolean hasChains()
      Description copied from interface: FilterChainManager
      Returns true if one or more configured chains are available, false if none are configured.
      Specified by:
      hasChains in interface FilterChainManager
      Returns:
      true if one or more configured chains are available, false if none are configured.
    • getChainNames

      Description copied from interface: FilterChainManager
      Returns the names of all configured chains or an empty Set if no chains have been configured.
      Specified by:
      getChainNames in interface FilterChainManager
      Returns:
      the names of all configured chains or an empty Set if no chains have been configured.
    • proxy

      public javax.servlet.FilterChain proxy(javax.servlet.FilterChain original, String chainName)
      Description copied from interface: FilterChainManager
      Proxies the specified original FilterChain with the named chain. The returned FilterChain instance will first execute the configured named chain and then lastly invoke the given original chain.
      Specified by:
      proxy in interface FilterChainManager
      Parameters:
      original - the original FilterChain to proxy
      chainName - the name of the internal configured filter chain that should 'sit in front' of the specified original chain.
      Returns:
      a FilterChain instance that will execute the named chain and then finally the specified original FilterChain instance.
    • initFilter

      protected void initFilter(javax.servlet.Filter filter)
      Initializes the filter by calling filter.init( getFilterConfig() );.
      Parameters:
      filter - the filter to initialize with the FilterConfig.
    • addDefaultFilters

      protected void addDefaultFilters(boolean init)