Package org.apache.shiro.web.servlet
Class OncePerRequestFilter
- java.lang.Object
-
- org.apache.shiro.web.servlet.ServletContextSupport
-
- org.apache.shiro.web.servlet.AbstractFilter
-
- org.apache.shiro.web.servlet.NameableFilter
-
- org.apache.shiro.web.servlet.OncePerRequestFilter
-
- All Implemented Interfaces:
javax.servlet.Filter,org.apache.shiro.util.Nameable
- Direct Known Subclasses:
AbstractShiroFilter,AdviceFilter
public abstract class OncePerRequestFilter extends NameableFilter
Filter base class that guarantees to be just executed once per request, on any servlet container. It provides adoFilterInternal(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)method with HttpServletRequest and HttpServletResponse arguments. ThegetAlreadyFilteredAttributeName()method determines how to identify that a request is already filtered. The default implementation is based on the configured name of the concrete filter instance.Controlling filter execution
1.2 introduced theisEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse)method andisEnabled()property to allow explicit control over whether the filter executes (or allows passthrough) for any given request. NOTE This class was initially borrowed from the Spring framework but has continued modifications.- Since:
- 0.1
-
-
Field Summary
Fields Modifier and Type Field Description static StringALREADY_FILTERED_SUFFIXSuffix that gets appended to the filter name for the "already filtered" request attribute.-
Fields inherited from class org.apache.shiro.web.servlet.AbstractFilter
filterConfig
-
-
Constructor Summary
Constructors Constructor Description OncePerRequestFilter()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voiddoFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain filterChain)ThisdoFilterimplementation stores a request attribute for "already filtered", proceeding without filtering again if the attribute is already there.protected abstract voiddoFilterInternal(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)Same contract as fordoFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain), but guaranteed to be invoked only once per request.protected StringgetAlreadyFilteredAttributeName()Return name of the request attribute that identifies that a request has already been filtered.booleanisEnabled()Returnstrueif this filter should generally* execute for any request,falseif it should let the request/response pass through immediately to the next element in theFilterChain.protected booleanisEnabled(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)Returnstrueif this filter should filter the specified request,falseif it should let the request/response pass through immediately to the next element in theFilterChain.booleanisFilterOncePerRequest()Returnstrueif this filter should only execute once per request.voidsetEnabled(boolean enabled)Sets whether or not this filter generally executes for any request.voidsetFilterOncePerRequest(boolean filterOncePerRequest)Sets whether this filter executes once per request or for every invocation of the filter.protected booleanshouldNotFilter(javax.servlet.ServletRequest request)Deprecated.in favor of overridingisEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse)for custom behavior.-
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
-
ALREADY_FILTERED_SUFFIX
public static final String ALREADY_FILTERED_SUFFIX
Suffix that gets appended to the filter name for the "already filtered" request attribute.
-
-
Method Detail
-
isEnabled
public boolean isEnabled()
Returnstrueif this filter should generally* execute for any request,falseif it should let the request/response pass through immediately to the next element in theFilterChain. The default value istrue, as most filters would inherently need to execute when configured. * This configuration property is for general configuration for any request that comes through the filter. TheisEnabled(request,response)method actually determines whether or not if the filter is enabled based on the current request.- Returns:
trueif this filter should generally execute,falseif it should let the request/response pass through immediately to the next element in theFilterChain.- Since:
- 1.2
-
setEnabled
public void setEnabled(boolean enabled)
Sets whether or not this filter generally executes for any request. See theisEnabled()JavaDoc as to what general execution means.- Parameters:
enabled- whether or not this filter generally executes.- Since:
- 1.2
-
isFilterOncePerRequest
public boolean isFilterOncePerRequest()
Returnstrueif this filter should only execute once per request. If set tofalsethis filter will execute each time it is invoked.- Returns:
trueif this filter should only execute once per request.- Since:
- 1.10
-
setFilterOncePerRequest
public void setFilterOncePerRequest(boolean filterOncePerRequest)
Sets whether this filter executes once per request or for every invocation of the filter. It is recommended to leave this disabled if you are using aRequestDispatcherto forward or include request (JSP tags, programmatically, or via a framework).- Parameters:
filterOncePerRequest- Whether this filter executes once per request.- Since:
- 1.10
-
doFilter
public final void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain filterChain) throws javax.servlet.ServletException, IOExceptionThisdoFilterimplementation stores a request attribute for "already filtered", proceeding without filtering again if the attribute is already there.- Throws:
javax.servlet.ServletExceptionIOException- See Also:
getAlreadyFilteredAttributeName(),shouldNotFilter(javax.servlet.ServletRequest),doFilterInternal(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
-
isEnabled
protected boolean isEnabled(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws javax.servlet.ServletException, IOExceptionReturnstrueif this filter should filter the specified request,falseif it should let the request/response pass through immediately to the next element in theFilterChain. This default implementation merely returns the value ofisEnabled(), which istrueby default (to ensure the filter always executes by default), but it can be overridden by subclasses for request-specific behavior if necessary. For example, a filter could be enabled or disabled based on the request path being accessed. Helpful Hint: if your subclass extendsPathMatchingFilter, you may wish to instead override thePathMatchingFilter.isEnabled(request,response,path,pathSpecificConfig)method if you want to make your enable/disable decision based on any path-specific configuration.- Parameters:
request- the incoming servlet requestresponse- the outbound servlet response- Returns:
trueif this filter should filter the specified request,falseif it should let the request/response pass through immediately to the next element in theFilterChain.- Throws:
IOException- in the case of any IO errorjavax.servlet.ServletException- in the case of any error- Since:
- 1.2
- See Also:
PathMatchingFilter.isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse, String, Object)
-
getAlreadyFilteredAttributeName
protected String getAlreadyFilteredAttributeName()
Return name of the request attribute that identifies that a request has already been filtered. The default implementation takes the configurednameand appends ".FILTERED". If the filter is not fully initialized, it falls back to the implementation's class name.- Returns:
- the name of the request attribute that identifies that a request has already been filtered.
- See Also:
NameableFilter.getName(),ALREADY_FILTERED_SUFFIX
-
shouldNotFilter
@Deprecated protected boolean shouldNotFilter(javax.servlet.ServletRequest request) throws javax.servlet.ServletException
Deprecated.in favor of overridingisEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse)for custom behavior. This method will be removed in Shiro 2.0.Can be overridden in subclasses for custom filtering control, returningtrueto avoid filtering of the given request.The default implementation always returns
false.- Parameters:
request- current HTTP request- Returns:
- whether the given request should not be filtered
- Throws:
javax.servlet.ServletException- in case of errors
-
doFilterInternal
protected abstract void doFilterInternal(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) throws javax.servlet.ServletException, IOExceptionSame contract as fordoFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain), but guaranteed to be invoked only once per request.- Parameters:
request- incomingServletRequestresponse- outgoingServletResponsechain- theFilterChainto execute- Throws:
javax.servlet.ServletException- if there is a problem processing the requestIOException- if there is an I/O problem processing the request
-
-