Package org.apache.shiro.web.filter.mgt
Class DefaultFilterChainManager
- java.lang.Object
-
- org.apache.shiro.web.filter.mgt.DefaultFilterChainManager
-
- All Implemented Interfaces:
FilterChainManager
public class DefaultFilterChainManager extends Object implements FilterChainManager
DefaultFilterChainManagerimplementation maintaining a map ofFilterinstances (key: filter name, value: Filter) as well as a map ofNamedFilterLists created from theseFilters (key: filter chain name, value: NamedFilterList). TheNamedFilterListis essentially aFilterChainthat also has a name property by which it can be looked up.- Since:
- 1.0
- See Also:
NamedFilterList
-
-
Constructor Summary
Constructors Constructor Description DefaultFilterChainManager()DefaultFilterChainManager(javax.servlet.FilterConfig filterConfig)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddDefaultFilters(boolean init)voidaddFilter(String name, javax.servlet.Filter filter)Adds a filter to the 'pool' of available filters that can be used whencreating filter chains.voidaddFilter(String name, javax.servlet.Filter filter, boolean init)Adds a filter to the 'pool' of available filters that can be used whencreating filter chains.protected voidaddFilter(String name, javax.servlet.Filter filter, boolean init, boolean overwrite)voidaddToChain(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)NamedFilterListgetChain(String chainName)Returns the filter chain identified by the specifiedchainNameornullif there is no chain with that name.Set<String>getChainNames()Returns the names of all configured chains or an emptySetif no chains have been configured.javax.servlet.FiltergetFilter(String name)Map<String,NamedFilterList>getFilterChains()javax.servlet.FilterConfiggetFilterConfig()Returns theFilterConfigprovided by the Servlet container at webapp startup.Map<String,javax.servlet.Filter>getFilters()Returns the pool of availableFilters managed by this manager, keyed byname.booleanhasChains()Returnstrueif 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.FilterChainproxy(javax.servlet.FilterChain original, String chainName)Proxies 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.
-
-
-
Method Detail
-
getFilterConfig
public javax.servlet.FilterConfig getFilterConfig()
Returns theFilterConfigprovided by the Servlet container at webapp startup.- Returns:
- the
FilterConfigprovided by the Servlet container at webapp startup.
-
setFilterConfig
public void setFilterConfig(javax.servlet.FilterConfig filterConfig)
Sets theFilterConfigprovided by the Servlet container at webapp startup.- Parameters:
filterConfig- theFilterConfigprovided by the Servlet container at webapp startup.
-
getFilters
public Map<String,javax.servlet.Filter> 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.
-
getFilterChains
public Map<String,NamedFilterList> 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: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
public void addFilter(String name, javax.servlet.Filter filter, boolean init)
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
public void createDefaultChain(String chainName)
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:
AntPathMatcher
-
createChain
public void createChain(String chainName, String chainDefinition)
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:
FilterChainResolver,AntPathMatcher
-
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:
- SHIRO-205
-
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:
- SHIRO-205
-
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: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
public void addToChain(String chainName, String filterName, String chainSpecificFilterConfig)
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 wll 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
protected NamedFilterList ensureChain(String chainName)
-
getChain
public NamedFilterList getChain(String chainName)
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
public boolean 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
public Set<String> 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
public javax.servlet.FilterChain proxy(javax.servlet.FilterChain original, String chainName)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
protected void initFilter(javax.servlet.Filter filter)
Initializes the filter by callingfilter.init(.getFilterConfig());- Parameters:
filter- the filter to initialize with theFilterConfig.
-
addDefaultFilters
protected void addDefaultFilters(boolean init)
-
-