Package org.apache.shiro.web.filter.mgt
Class DefaultFilterChainManager
java.lang.Object
org.apache.shiro.web.filter.mgt.DefaultFilterChainManager
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddDefaultFilters(boolean init) voidAdds a filter to the 'pool' of available filters that can be used whencreating filter chains.voidAdds a filter to the 'pool' of available filters that can be used whencreating filter chains.protected voidvoidaddToChain(String chainName, String filterName) Adds (appends) a filter to the filter chain identified by the givenchainName.voidaddToChain(String chainName, String filterName, String chainSpecificFilterConfig) Adds (appends) a filter to the filter chain identified by the givenchainName.protected voidapplyChainConfig(String chainName, javax.servlet.Filter filter, String chainSpecificFilterConfig) voidcreateChain(String chainName, String chainDefinition) Creates a filter chain for the givenchainNamewith the specifiedchainDefinitionString.voidcreateDefaultChain(String chainName) Creates a chain that should match any non-matched request paths, typically/**assuming anAntPathMatcherI used.protected NamedFilterListensureChain(String chainName) Returns the filter chain identified by the specifiedchainNameornullif there is no chain with that name.Returns the names of all configured chains or an emptySetif no chains have been configured.javax.servlet.Filterjavax.servlet.FilterConfigReturns theFilterConfigprovided by the Servlet container at webapp startup.Returns the pool of availableFilters managed by this manager, keyed byname.booleanReturnstrueif one or more configured chains are available,falseif none are configured.protected voidinitFilter(javax.servlet.Filter filter) Initializes the filter by callingfilter.init(.getFilterConfig());javax.servlet.FilterChainProxies the specifiedoriginalFilterChain with the named chain.voidsetFilterChains(Map<String, NamedFilterList> filterChains) voidsetFilterConfig(javax.servlet.FilterConfig filterConfig) Sets theFilterConfigprovided by the Servlet container at webapp startup.voidsetFilters(Map<String, javax.servlet.Filter> filters) voidsetGlobalFilters(List<String> globalFilterNames) Configures the set of named filters that will match all paths.protected String[]splitChainDefinition(String chainDefinition) Splits the comma-delimited filter chain definition line into individual filter definition tokens.protected String[]toNameConfigPair(String token) Based on the given filter chain definition token (e.g.
-
Constructor Details
-
DefaultFilterChainManager
public DefaultFilterChainManager() -
DefaultFilterChainManager
-
-
Method Details
-
getFilterConfig
Returns theFilterConfigprovided by the Servlet container at webapp startup.- Returns:
- the
FilterConfigprovided by the Servlet container at webapp startup.
-
setFilterConfig
Sets theFilterConfigprovided by the Servlet container at webapp startup.- Parameters:
filterConfig- theFilterConfigprovided by the Servlet container at webapp startup.
-
getFilters
Description copied from interface:FilterChainManagerReturns the pool of availableFilters managed by this manager, keyed byname.- Specified by:
getFiltersin interfaceFilterChainManager- Returns:
- the pool of available
Filters managed by this manager, keyed byname.
-
setFilters
-
getFilterChains
-
setFilterChains
-
getFilter
-
addFilter
Description copied from interface:FilterChainManagerAdds a filter to the 'pool' of available filters that can be used whencreating filter chains. Calling this method is effectively the same as callingaddFilter(name, filter, false);- Specified by:
addFilterin interfaceFilterChainManager- Parameters:
name- the name to assign to the filter, used to reference the filter in chain definitionsfilter- the filter to initialize and then add to the pool of available filters that can be used
-
addFilter
Description copied from interface:FilterChainManagerAdds a filter to the 'pool' of available filters that can be used whencreating filter chains.- Specified by:
addFilterin interfaceFilterChainManager- Parameters:
name- the name to assign to the filter, used to reference the filter in chain definitionsfilter- the filter to assign to the filter poolinit- whether or not theFiltershould beinitializedfirst before being added to the pool.
-
createDefaultChain
Description copied from interface:FilterChainManagerCreates a chain that should match any non-matched request paths, typically/**assuming anAntPathMatcherI used.- Specified by:
createDefaultChainin interfaceFilterChainManager- Parameters:
chainName- The name of the chain to create, likely/**.- See Also:
-
createChain
Description copied from interface:FilterChainManagerCreates a filter chain for the givenchainNamewith the specifiedchainDefinitionString.Conventional Use
Because theFilterChainManagerinterface 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 anAnt path expression). For example:createChain(path_expression, path_specific_filter_chain_definition);This convention can be used by aFilterChainResolverto 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
ThechainDefinitionmethod argument is expected to conform to the following format:filter1[optional_config1], filter2[optional_config2], ..., filterN[optional_configN]
wherefilterNis the name of a filter previouslyregisteredwith the manager, and[optional_configN]is an optional bracketed string that has meaning for that particular filter for this particular chain
filterN[]just becomesfilterN. And because this method does create a chain, remember that order matters! The comma-delimited filter tokens in thechainDefinitionspecify the chain's execution order.Examples
/account/** = authcBasic
This example says "Create a filter named '/account/**' consisting of only the 'authcBasic' filter". Also because theauthcBasicfilter 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- ensures the user is first authenticated (
authcBasic) then - ensures that user has the
b2bClientrole, and then finally - ensures that they have the
remote:invoke:lan,wanpermission.
- authcBasic
- roles[b2bclient]
- perms[remote:invoke:lan
- wan]
- Specified by:
createChainin interfaceFilterChainManager- Parameters:
chainName- the name to associate with the chain, conventionally a URL path pattern.chainDefinition- the string-formatted chain definition used to construct an actualNamedFilterListchain instance.- See Also:
-
splitChainDefinition
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 fooreturned[0] == foo
returned[1] ==nullfoo[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
-
addToChain
Description copied from interface:FilterChainManagerAdds (appends) a filter to the filter chain identified by the givenchainName. 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:
addToChainin interfaceFilterChainManager- Parameters:
chainName- the name of the chain where the filter will be appended.filterName- the name of theregisteredfilter to add to the chain.
-
addToChain
Description copied from interface:FilterChainManagerAdds (appends) a filter to the filter chain identified by the givenchainName. 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 aPathConfigProcessorto accept per-chain configuration. If it is not, aIllegalArgumentExceptionwill be thrown.- Specified by:
addToChainin interfaceFilterChainManager- Parameters:
chainName- the name of the chain where the filter will be appended.filterName- the name of theregisteredfilter 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:FilterChainManagerConfigures the set of named filters that will match all paths. These filters will match BEFORE explicitly configured filter chains i.e. by callingFilterChainManager.createChain(String, String),FilterChainManager.addToChain(String, String), etc.
Filters configured in this list will apply to ALL requests.- Specified by:
setGlobalFiltersin interfaceFilterChainManager- 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 filtersFilterChainManager.getFilters()}.
-
applyChainConfig
protected void applyChainConfig(String chainName, javax.servlet.Filter filter, String chainSpecificFilterConfig) -
ensureChain
-
getChain
Description copied from interface:FilterChainManagerReturns the filter chain identified by the specifiedchainNameornullif there is no chain with that name.- Specified by:
getChainin interfaceFilterChainManager- Parameters:
chainName- the name identifying the filter chain.- Returns:
- the filter chain identified by the specified
chainNameornullif there is no chain with that name.
-
hasChains
Description copied from interface:FilterChainManagerReturnstrueif one or more configured chains are available,falseif none are configured.- Specified by:
hasChainsin interfaceFilterChainManager- Returns:
trueif one or more configured chains are available,falseif none are configured.
-
getChainNames
Description copied from interface:FilterChainManagerReturns the names of all configured chains or an emptySetif no chains have been configured.- Specified by:
getChainNamesin interfaceFilterChainManager- Returns:
- the names of all configured chains or an empty
Setif no chains have been configured.
-
proxy
Description copied from interface:FilterChainManagerProxies the specifiedoriginalFilterChain with the named chain. The returnedFilterChaininstance will first execute the configured named chain and then lastly invoke the givenoriginalchain.- Specified by:
proxyin interfaceFilterChainManager- Parameters:
original- the original FilterChain to proxychainName- the name of the internal configured filter chain that should 'sit in front' of the specified original chain.- Returns:
- a
FilterChaininstance that will execute the named chain and then finally the specifiedoriginalFilterChain instance.
-
initFilter
Initializes the filter by callingfilter.init(.getFilterConfig());- Parameters:
filter- the filter to initialize with theFilterConfig.
-
addDefaultFilters
-