类 OncePerRequestFilter
- java.lang.Object
- org.springframework.web.filter.GenericFilterBean
- org.springframework.web.filter.OncePerRequestFilter
- 所有已实现的接口:
Filter
,Aware
,BeanNameAware
,DisposableBean
,InitializingBean
,EnvironmentAware
,EnvironmentCapable
,ServletContextAware
- 直接已知子类:
AbstractRequestLoggingFilter
,CharacterEncodingFilter
,CorsFilter
,FormContentFilter
,ForwardedHeaderFilter
,HiddenHttpMethodFilter
,HttpPutFormContentFilter
,MultipartFilter
,OpenEntityManagerInViewFilter
,OpenSessionInViewFilter
,RelativeRedirectFilter
,RequestContextFilter
,ShallowEtagHeaderFilter
public abstract class OncePerRequestFilter extends GenericFilterBean
Filter base class that aims to guarantee a single execution per request dispatch, on any servlet container. It provides adoFilterInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
method with HttpServletRequest and HttpServletResponse arguments.As of Servlet 3.0, a filter may be invoked as part of a
REQUEST
orASYNC
dispatches that occur in separate threads. A filter can be configured inweb.xml
whether it should be involved in async dispatches. However, in some cases servlet containers assume different default configuration. Therefore sub-classes can override the methodshouldNotFilterAsyncDispatch()
to declare statically if they should indeed be invoked, once, during both types of dispatches in order to provide thread initialization, logging, security, and so on. This mechanism complements and does not replace the need to configure a filter inweb.xml
with dispatcher types.Subclasses may use
isAsyncDispatch(HttpServletRequest)
to determine when a filter is invoked as part of an async dispatch, and useisAsyncStarted(HttpServletRequest)
to determine when the request has been placed in async mode and therefore the current dispatch won't be the last one for the given request.Yet another dispatch type that also occurs in its own thread is
ERROR
. Subclasses can overrideshouldNotFilterErrorDispatch()
if they wish to declare statically if they should be invoked once during error dispatches.The
getAlreadyFilteredAttributeName()
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.- 从以下版本开始:
- 06.12.2003
- 作者:
- Juergen Hoeller, Rossen Stoyanchev
字段概要
字段 修饰符和类型 字段 说明 static String
ALREADY_FILTERED_SUFFIX
Suffix that gets appended to the filter name for the "already filtered" request attribute.从类继承的字段 org.springframework.web.filter.GenericFilterBean
logger
构造器概要
构造器 构造器 说明 OncePerRequestFilter()
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 void
doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
ThisdoFilter
implementation stores a request attribute for "already filtered", proceeding without filtering again if the attribute is already there.protected abstract void
doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
Same contract as fordoFilter
, but guaranteed to be just invoked once per request within a single request thread.protected void
doFilterNestedErrorDispatch(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
Typically an ERROR dispatch happens after the REQUEST dispatch completes, and the filter chain starts anew.protected String
getAlreadyFilteredAttributeName()
Return the name of the request attribute that identifies that a request is already filtered.protected boolean
isAsyncDispatch(HttpServletRequest request)
The dispatcher typejavax.servlet.DispatcherType.ASYNC
introduced in Servlet 3.0 means a filter can be invoked in more than one thread over the course of a single request.protected boolean
isAsyncStarted(HttpServletRequest request)
Whether request processing is in asynchronous mode meaning that the response will not be committed after the current thread is exited.protected boolean
shouldNotFilter(HttpServletRequest request)
Can be overridden in subclasses for custom filtering control, returningtrue
to avoid filtering of the given request.protected boolean
shouldNotFilterAsyncDispatch()
The dispatcher typejavax.servlet.DispatcherType.ASYNC
introduced in Servlet 3.0 means a filter can be invoked in more than one thread over the course of a single request.protected boolean
shouldNotFilterErrorDispatch()
Whether to filter error dispatches such as when the servlet container processes and error mapped inweb.xml
.从类继承的方法 org.springframework.web.filter.GenericFilterBean
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
字段详细资料
ALREADY_FILTERED_SUFFIX
public static final String ALREADY_FILTERED_SUFFIX
Suffix that gets appended to the filter name for the "already filtered" request attribute.
构造器详细资料
OncePerRequestFilter
public OncePerRequestFilter()
方法详细资料
doFilter
public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException
ThisdoFilter
implementation stores a request attribute for "already filtered", proceeding without filtering again if the attribute is already there.- 参数:
request
- theServletRequest
object contains the client's requestresponse
- theServletResponse
object contains the filter's responsefilterChain
- theFilterChain
for invoking the next filter or the resource- 抛出:
ServletException
- if an exception occurs that interferes with the filter's normal operationIOException
- if an I/O related error has occurred during the processing- 另请参阅:
getAlreadyFilteredAttributeName()
,shouldNotFilter(javax.servlet.http.HttpServletRequest)
,doFilterInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
isAsyncDispatch
protected boolean isAsyncDispatch(HttpServletRequest request)
The dispatcher typejavax.servlet.DispatcherType.ASYNC
introduced in Servlet 3.0 means a filter can be invoked in more than one thread over the course of a single request. This method returnstrue
if the filter is currently executing within an asynchronous dispatch.- 参数:
request
- the current request- 从以下版本开始:
- 3.2
- 另请参阅:
WebAsyncManager.hasConcurrentResult()
isAsyncStarted
protected boolean isAsyncStarted(HttpServletRequest request)
Whether request processing is in asynchronous mode meaning that the response will not be committed after the current thread is exited.- 参数:
request
- the current request- 从以下版本开始:
- 3.2
- 另请参阅:
WebAsyncManager.isConcurrentHandlingStarted()
getAlreadyFilteredAttributeName
protected String getAlreadyFilteredAttributeName()
Return the name of the request attribute that identifies that a request is already filtered.The default implementation takes the configured name of the concrete filter instance and appends ".FILTERED". If the filter is not fully initialized, it falls back to its class name.
shouldNotFilter
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException
Can be overridden in subclasses for custom filtering control, returningtrue
to avoid filtering of the given request.The default implementation always returns
false
.- 参数:
request
- current HTTP request- 返回:
- whether the given request should not be filtered
- 抛出:
ServletException
- in case of errors
shouldNotFilterAsyncDispatch
protected boolean shouldNotFilterAsyncDispatch()
The dispatcher typejavax.servlet.DispatcherType.ASYNC
introduced in Servlet 3.0 means a filter can be invoked in more than one thread over the course of a single request. Some filters only need to filter the initial thread (e.g. request wrapping) while others may need to be invoked at least once in each additional thread for example for setting up thread locals or to perform final processing at the very end.Note that although a filter can be mapped to handle specific dispatcher types via
web.xml
or in Java through theServletContext
, servlet containers may enforce different defaults with regards to dispatcher types. This flag enforces the design intent of the filter.The default return value is "true", which means the filter will not be invoked during subsequent async dispatches. If "false", the filter will be invoked during async dispatches with the same guarantees of being invoked only once during a request within a single thread.
- 从以下版本开始:
- 3.2
shouldNotFilterErrorDispatch
protected boolean shouldNotFilterErrorDispatch()
Whether to filter error dispatches such as when the servlet container processes and error mapped inweb.xml
. The default return value is "true", which means the filter will not be invoked in case of an error dispatch.- 从以下版本开始:
- 3.2
doFilterInternal
protected abstract void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
Same contract as fordoFilter
, but guaranteed to be just invoked once per request within a single request thread. SeeshouldNotFilterAsyncDispatch()
for details.Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.
doFilterNestedErrorDispatch
protected void doFilterNestedErrorDispatch(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
Typically an ERROR dispatch happens after the REQUEST dispatch completes, and the filter chain starts anew. On some servers however the ERROR dispatch may be nested within the REQUEST dispatch, e.g. as a result of callingsendError
on the response. In that case we are still in the filter chain, on the same thread, but the request and response have been switched to the original, unwrapped ones.Sub-classes may use this method to filter such nested ERROR dispatches and re-apply wrapping on the request or response.
ThreadLocal
context, if any, should still be active as we are still nested within the filter chain.- 抛出:
ServletException
IOException
- 从以下版本开始:
- 5.1.9