OncePerRequestFilter的作用
生活随笔
收集整理的這篇文章主要介紹了
OncePerRequestFilter的作用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在Spring中,filter默認繼承OncePerRequestFilter,
OncePerRequestFilter源代碼如下:
/** Copyright 2002-2008 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.web.filter;import java.io.IOException;import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** Filter base class that guarantees to be just executed once per request,* on any servlet container. It provides a {@link #doFilterInternal}* method with HttpServletRequest and HttpServletResponse arguments.** <p>The {@link #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.** @author Juergen Hoeller* @since 06.12.2003*/ public abstract class OncePerRequestFilter extends GenericFilterBean {/*** Suffix that gets appended to the filter name for the* "already filtered" request attribute.* @see #getAlreadyFilteredAttributeName*/public static final String ALREADY_FILTERED_SUFFIX = ".FILTERED";/*** This <code>doFilter</code> implementation stores a request attribute for* "already filtered", proceeding without filtering again if the* attribute is already there.* @see #getAlreadyFilteredAttributeName* @see #shouldNotFilter* @see #doFilterInternal*/public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)throws ServletException, IOException {if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {throw new ServletException("OncePerRequestFilter just supports HTTP requests");}HttpServletRequest httpRequest = (HttpServletRequest) request;HttpServletResponse httpResponse = (HttpServletResponse) response;String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();if (request.getAttribute(alreadyFilteredAttributeName) != null || shouldNotFilter(httpRequest)) {// Proceed without invoking this filter... filterChain.doFilter(request, response);}else {// Do invoke this filter... request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);try {doFilterInternal(httpRequest, httpResponse, filterChain);}finally {// Remove the "already filtered" request attribute for this request. request.removeAttribute(alreadyFilteredAttributeName);}}}/*** Return the name of the request attribute that identifies that a request* is already filtered.* <p>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.* @see #getFilterName* @see #ALREADY_FILTERED_SUFFIX*/protected String getAlreadyFilteredAttributeName() {String name = getFilterName();if (name == null) {name = getClass().getName();}return name + ALREADY_FILTERED_SUFFIX;}/*** Can be overridden in subclasses for custom filtering control,* returning <code>true</code> to avoid filtering of the given request.* <p>The default implementation always returns <code>false</code>.* @param request current HTTP request* @return whether the given request should <i>not</i> be filtered* @throws ServletException in case of errors*/protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {return false;}/*** Same contract as for <code>doFilter</code>, but guaranteed to be* just invoked once per request. Provides HttpServletRequest and* HttpServletResponse arguments instead of the default ServletRequest* and ServletResponse ones.*/protected abstract void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)throws ServletException, IOException;}OncePerRequestFilter,顧名思義,它能夠確保在一次請求中只通過一次filter,而需要重復的執(zhí)行。大家常識上都認為,一次請求本來就只filter一次,為什么還要由此特別限定呢,往往我們的常識和實際的實現(xiàn)并不真的一樣,經(jīng)過一番資料的查閱,此方法是為了兼容不同的web container,也就是說并不是所有的container都入我們期望的只過濾一次,servlet版本不同,執(zhí)行過程也不同,
請看官方對OncePerRequestFilter的注釋:
/*** Filter base class that guarantees to be just executed once per request,* on any servlet container. It provides a {@link #doFilterInternal}* method with HttpServletRequest and HttpServletResponse arguments.** <p>The {@link #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.** @author Juergen Hoeller* @since 06.12.2003*/如:servlet2.3與servlet2.4也有一定差異:
在servlet2.3中,Filter會經(jīng)過一切請求,包括服務器內(nèi)部使用的forward轉(zhuǎn)發(fā)請求和<%@ include file=”/login.jsp”%>的情況servlet2.4中的Filter默認情況下只過濾外部提交的請求,forward和include這些內(nèi)部轉(zhuǎn)發(fā)都不會被過濾,因此,為了兼容各種不同運行環(huán)境和版本,默認filter繼承OncePerRequestFilter是一個比較穩(wěn)妥的選擇
轉(zhuǎn)載于:https://www.cnblogs.com/shanshouchen/archive/2012/07/31/2617412.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的OncePerRequestFilter的作用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简明 ES6 模块
- 下一篇: Android--pendingInte