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
DefaultFilterChainManager
implementation maintaining a map ofFilter
instances (key: filter name, value: Filter) as well as a map ofNamedFilterList
s created from theseFilter
s (key: filter chain name, value: NamedFilterList). TheNamedFilterList
is essentially aFilterChain
that 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(FilterConfig filterConfig)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addDefaultFilters(boolean init)
void
addFilter(String name, Filter filter)
Adds a filter to the 'pool' of available filters that can be used whencreating filter chains
.void
addFilter(String name, Filter filter, boolean init)
Adds a filter to the 'pool' of available filters that can be used whencreating filter chains
.protected void
addFilter(String name, Filter filter, boolean init, boolean overwrite)
void
addToChain(String chainName, String filterName)
Adds (appends) a filter to the filter chain identified by the givenchainName
.void
addToChain(String chainName, String filterName, String chainSpecificFilterConfig)
Adds (appends) a filter to the filter chain identified by the givenchainName
.protected void
applyChainConfig(String chainName, Filter filter, String chainSpecificFilterConfig)
void
createChain(String chainName, String chainDefinition)
Creates a filter chain for the givenchainName
with the specifiedchainDefinition
String.void
createDefaultChain(String chainName)
Creates a chain that should match any non-matched request paths, typically/**
assuming anAntPathMatcher
I used.protected NamedFilterList
ensureChain(String chainName)
NamedFilterList
getChain(String chainName)
Returns the filter chain identified by the specifiedchainName
ornull
if there is no chain with that name.Set<String>
getChainNames()
Returns the names of all configured chains or an emptySet
if no chains have been configured.Filter
getFilter(String name)
Map<String,NamedFilterList>
getFilterChains()
FilterConfig
getFilterConfig()
Returns theFilterConfig
provided by the Servlet container at webapp startup.Map<String,Filter>
getFilters()
Returns the pool of availableFilter
s managed by this manager, keyed byname
.boolean
hasChains()
Returnstrue
if one or more configured chains are available,false
if none are configured.protected void
initFilter(Filter filter)
Initializes the filter by callingfilter.init(
.getFilterConfig()
);FilterChain
proxy(FilterChain original, String chainName)
Proxies the specifiedoriginal
FilterChain with the named chain.void
setFilterChains(Map<String,NamedFilterList> filterChains)
void
setFilterConfig(FilterConfig filterConfig)
Sets theFilterConfig
provided by the Servlet container at webapp startup.void
setFilters(Map<String,Filter> filters)
void
setGlobalFilters(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 Detail
-
DefaultFilterChainManager
public DefaultFilterChainManager()
-
DefaultFilterChainManager
public DefaultFilterChainManager(FilterConfig filterConfig)
-
-
Method Detail
-
getFilterConfig
public FilterConfig getFilterConfig()
Returns theFilterConfig
provided by the Servlet container at webapp startup.- Returns:
- the
FilterConfig
provided by the Servlet container at webapp startup.
-
setFilterConfig
public void setFilterConfig(FilterConfig filterConfig)
Sets theFilterConfig
provided by the Servlet container at webapp startup.- Parameters:
filterConfig
- theFilterConfig
provided by the Servlet container at webapp startup.
-
getFilters
public Map<String,Filter> getFilters()
Description copied from interface:FilterChainManager
Returns the pool of availableFilter
s managed by this manager, keyed byname
.- Specified by:
getFilters
in interfaceFilterChainManager
- Returns:
- the pool of available
Filter
s managed by this manager, keyed byname
.
-
setFilters
public void setFilters(Map<String,Filter> filters)
-
getFilterChains
public Map<String,NamedFilterList> getFilterChains()
-
setFilterChains
public void setFilterChains(Map<String,NamedFilterList> filterChains)
-
addFilter
public void addFilter(String name, Filter filter)
Description copied from interface:FilterChainManager
Adds 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:
addFilter
in 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, Filter filter, boolean init)
Description copied from interface:FilterChainManager
Adds a filter to the 'pool' of available filters that can be used whencreating filter chains
.- Specified by:
addFilter
in 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 theFilter
should beinitialized
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 anAntPathMatcher
I used.- Specified by:
createDefaultChain
in 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:FilterChainManager
Creates a filter chain for the givenchainName
with the specifiedchainDefinition
String.Conventional Use
Because theFilterChainManager
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 anAnt path expression
). For example:createChain(path_expression, path_specific_filter_chain_definition);
This convention can be used by aFilterChainResolver
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
ThechainDefinition
method argument is expected to conform to the following format:filter1[optional_config1], filter2[optional_config2], ..., filterN[optional_configN]
wherefilterN
is the name of a filter previouslyregistered
with 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 thechainDefinition
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 theauthcBasic
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- ensures the user is first authenticated (
authcBasic
) then - ensures that user has the
b2bClient
role, and then finally - ensures that they have the
remote:invoke:lan,wan
permission.
- authcBasic
- roles[b2bclient]
- perms[remote:invoke:lan
- wan]
- Specified by:
createChain
in 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 actualNamedFilterList
chain 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 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:
ConfigurationException
- if the token cannot be parsed- Since:
- 1.2
- See Also:
- SHIRO-205
-
addToChain
public void addToChain(String chainName, String filterName)
Description copied from interface:FilterChainManager
Adds (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:
addToChain
in interfaceFilterChainManager
- Parameters:
chainName
- the name of the chain where the filter will be appended.filterName
- the name of theregistered
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 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 aPathConfigProcessor
to accept per-chain configuration. If it is not, aIllegalArgumentException
will be thrown.- Specified by:
addToChain
in interfaceFilterChainManager
- Parameters:
chainName
- the name of the chain where the filter will be appended.filterName
- the name of theregistered
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 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 callingFilterChainManager.createChain(String, String)
,FilterChainManager.addToChain(String, String)
, etc.
Filters configured in this list wll apply to ALL requests.- Specified by:
setGlobalFilters
in interfaceFilterChainManager
- Parameters:
globalFilterNames
- the list of filter names to match ALL paths.- Throws:
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, 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 specifiedchainName
ornull
if there is no chain with that name.- Specified by:
getChain
in interfaceFilterChainManager
- Parameters:
chainName
- the name identifying the filter chain.- Returns:
- the filter chain identified by the specified
chainName
ornull
if there is no chain with that name.
-
hasChains
public boolean hasChains()
Description copied from interface:FilterChainManager
Returnstrue
if one or more configured chains are available,false
if none are configured.- Specified by:
hasChains
in interfaceFilterChainManager
- Returns:
true
if one or more configured chains are available,false
if none are configured.
-
getChainNames
public Set<String> getChainNames()
Description copied from interface:FilterChainManager
Returns the names of all configured chains or an emptySet
if no chains have been configured.- Specified by:
getChainNames
in interfaceFilterChainManager
- Returns:
- the names of all configured chains or an empty
Set
if no chains have been configured.
-
proxy
public FilterChain proxy(FilterChain original, String chainName)
Description copied from interface:FilterChainManager
Proxies the specifiedoriginal
FilterChain with the named chain. The returnedFilterChain
instance will first execute the configured named chain and then lastly invoke the givenoriginal
chain.- Specified by:
proxy
in 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
FilterChain
instance that will execute the named chain and then finally the specifiedoriginal
FilterChain instance.
-
initFilter
protected void initFilter(Filter filter)
Initializes the filter by callingfilter.init(
.getFilterConfig()
);- Parameters:
filter
- the filter to initialize with theFilterConfig
.
-
addDefaultFilters
protected void addDefaultFilters(boolean init)
-
-