Package org.apache.shiro.web.filter
Class PathMatchingFilter
-
- All Implemented Interfaces:
Filter
,Nameable
,PathConfigProcessor
- Direct Known Subclasses:
AccessControlFilter
,AnonymousFilter
,NoSessionCreationFilter
public abstract class PathMatchingFilter extends AdviceFilter implements PathConfigProcessor
Base class for Filters that will process only specified paths and allow all others to pass through.
- Since:
- 0.9
-
-
Field Summary
Fields Modifier and Type Field Description protected Map<String,Object>
appliedPaths
A collection of path-to-config entries where the key is a path which this filter should process and the value is the (possibly null) configuration element specific to this Filter for that specific path.protected PatternMatcher
pathMatcher
PatternMatcher used in determining which paths to react to for a given request.-
Fields inherited from class org.apache.shiro.web.servlet.OncePerRequestFilter
ALREADY_FILTERED_SUFFIX
-
Fields inherited from class org.apache.shiro.web.servlet.AbstractFilter
filterConfig
-
-
Constructor Summary
Constructors Constructor Description PathMatchingFilter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected String
getPathWithinApplication(ServletRequest request)
Returns the context path within the application based on the specifiedrequest
.protected boolean
isEnabled(ServletRequest request, ServletResponse response, String path, Object mappedValue)
Path-matching version of the parent class'sOncePerRequestFilter.isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
method, but additionally allows for inspection of any path-specific configuration values corresponding to the specified request.protected boolean
onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue)
This default implementation always returnstrue
and should be overridden by subclasses for custom logic if necessary.protected boolean
pathsMatch(String pattern, String path)
Returnstrue
if thepath
matches the specifiedpattern
string,false
otherwise.protected boolean
pathsMatch(String path, ServletRequest request)
Returnstrue
if the incomingrequest
matches the specifiedpath
pattern,false
otherwise.protected boolean
preHandle(ServletRequest request, ServletResponse response)
Implementation that handles path-matching behavior before a request is evaluated.Filter
processPathConfig(String path, String config)
Splits any comma-delmited values that might be found in theconfig
argument and sets the resultingString[]
array on theappliedPaths
internal Map.-
Methods inherited from class org.apache.shiro.web.servlet.AdviceFilter
afterCompletion, cleanup, doFilterInternal, executeChain, postHandle
-
Methods inherited from class org.apache.shiro.web.servlet.OncePerRequestFilter
doFilter, getAlreadyFilteredAttributeName, isEnabled, isEnabled, isFilterOncePerRequest, setEnabled, setFilterOncePerRequest, shouldNotFilter
-
Methods inherited from class org.apache.shiro.web.servlet.NameableFilter
getName, setName, toStringBuilder
-
Methods inherited from class org.apache.shiro.web.servlet.AbstractFilter
destroy, getFilterConfig, getInitParam, init, onFilterConfigSet, setFilterConfig
-
Methods inherited from class org.apache.shiro.web.servlet.ServletContextSupport
getContextAttribute, getContextInitParam, getServletContext, removeContextAttribute, setContextAttribute, setServletContext, toString
-
-
-
-
Field Detail
-
pathMatcher
protected PatternMatcher pathMatcher
PatternMatcher used in determining which paths to react to for a given request.
-
appliedPaths
protected Map<String,Object> appliedPaths
A collection of path-to-config entries where the key is a path which this filter should process and the value is the (possibly null) configuration element specific to this Filter for that specific path.To put it another way, the keys are the paths (urls) that this Filter will process.
The values are filter-specific data that this Filter should use when processing the corresponding key (path). The values can be null if no Filter-specific config was specified for that url.
-
-
Constructor Detail
-
PathMatchingFilter
public PathMatchingFilter()
-
-
Method Detail
-
processPathConfig
public Filter processPathConfig(String path, String config)
Splits any comma-delmited values that might be found in theconfig
argument and sets the resultingString[]
array on theappliedPaths
internal Map. That is:String[] values = null; if (config != null) { values = split(config); } this.
appliedPaths
.put(path, values);- Specified by:
processPathConfig
in interfacePathConfigProcessor
- Parameters:
path
- the application context path to match for executing this filter.config
- the specified for this particular filter only for the givenpath
- Returns:
- this configured filter.
-
getPathWithinApplication
protected String getPathWithinApplication(ServletRequest request)
Returns the context path within the application based on the specifiedrequest
. This implementation merely delegates toWebUtils.getPathWithinApplication(request)
, but can be overridden by subclasses for custom logic.- Parameters:
request
- the incomingServletRequest
- Returns:
- the context path within the application.
-
pathsMatch
protected boolean pathsMatch(String path, ServletRequest request)
Returnstrue
if the incomingrequest
matches the specifiedpath
pattern,false
otherwise. The default implementation acquires therequest
's path within the application and determines if that matches:String requestURI =
getPathWithinApplication(request)
;
returnpathsMatch(path,requestURI)
- Parameters:
path
- the configured url pattern to check the incoming request against.request
- the incoming ServletRequest- Returns:
true
if the incomingrequest
matches the specifiedpath
pattern,false
otherwise.
-
pathsMatch
protected boolean pathsMatch(String pattern, String path)
Returnstrue
if thepath
matches the specifiedpattern
string,false
otherwise. Simply delegates tothis.pathMatcher.
, but can be overridden by subclasses for custom matching behavior.matches(pattern,path)
- Parameters:
pattern
- the pattern to match againstpath
- the value to match with the specifiedpattern
- Returns:
true
if thepath
matches the specifiedpattern
string,false
otherwise.
-
preHandle
protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception
Implementation that handles path-matching behavior before a request is evaluated. If the path matches and the filterisEnabled
for that path/config, the request will be allowed through via the result fromonPreHandle
. If the path does not match or the filter is not enabled for that path, this filter will allow passthrough immediately to allow theFilterChain
to continue executing. In order to retain path-matching functionality, subclasses should not override this method if at all possible, and instead overrideonPreHandle
instead.- Overrides:
preHandle
in classAdviceFilter
- Parameters:
request
- the incoming ServletRequestresponse
- the outgoing ServletResponse- Returns:
true
if the filter chain is allowed to continue to execute,false
if a subclass has handled the request explicitly.- Throws:
Exception
- if an error occurs
-
onPreHandle
protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception
This default implementation always returnstrue
and should be overridden by subclasses for custom logic if necessary.- Parameters:
request
- the incoming ServletRequestresponse
- the outgoing ServletResponsemappedValue
- the filter-specific config value mapped to this filter in the URL rules mappings.- Returns:
true
if the request should be able to continue,false
if the filter will handle the response directly.- Throws:
Exception
- if an error occurs- See Also:
isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse, String, Object)
-
isEnabled
protected boolean isEnabled(ServletRequest request, ServletResponse response, String path, Object mappedValue) throws Exception
Path-matching version of the parent class'sOncePerRequestFilter.isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
method, but additionally allows for inspection of any path-specific configuration values corresponding to the specified request. Subclasses may wish to inspect this additional mapped configuration to determine if the filter is enabled or not. This method's default implementation ignores thepath
andmappedValue
arguments and merely returns the value from a call toOncePerRequestFilter.isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
. It is expected that subclasses override this method if they need to perform enable/disable logic for a specific request based on any path-specific config for the filter instance.- Parameters:
request
- the incoming servlet requestresponse
- the outbound servlet responsepath
- the path matched for the incoming servlet request that has been configured with the givenmappedValue
.mappedValue
- the filter-specific config value mapped to this filter in the URL rules mappings for the givenpath
.- Returns:
true
if this filter should filter the specified request,false
if it should let the request/response pass through immediately to the next element in theFilterChain
.- Throws:
Exception
- in the case of any error- Since:
- 1.2
-
-