OncePerRequestFilter的作用

在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,而需要重复的执行。大家常识上都认为,一次请求本来就只filter一次,为什么还要由此特别限定呢,往往我们的常识和实际的实现并不真的一样,经过一番资料的查阅,此方法是为了兼容不同的web container,也就是说并不是所有的container都入我们期望的只过滤一次,servlet版本不同,执行过程也不同,

请看官方对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会经过一切请求,包括服务器内部使用的forward转发请求和<%@ include file=”/login.jsp”%>的情况servlet2.4中的Filter默认情况下只过滤外部提交的请求,forward和include这些内部转发都不会被过滤,

因此,为了兼容各种不同运行环境和版本,默认filter继承OncePerRequestFilter是一个比较稳妥的选择

转载于:https://www.cnblogs.com/shanshouchen/archive/2012/07/31/2617412.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/461830.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++远航之封装篇——构造函数

1、为什么需要构造函数&#xff1f; 见博客http://blog.csdn.net/zhhymh/article/details/6236317 2、c中的内存分区 &#xff08;1&#xff09;栈区 int x0&#xff1b;int *pNULL&#xff1b; &#xff08;2&#xff09;堆区 int *p new int[20]; &#xff08;3&#xff0…

Vim基本操作总结

本文是学习Vim时的笔记总结&#xff0c;以便在遗忘时方便查找相关命令&#xff0c;原学习视频链接&#xff1a;https://www.imooc.com/learn/1129 1.1 Vim的4种模式 1.normal模式&#xff1a;使用i/a/o以及I/A/O进入插入模式&#xff0c;其中各快捷键的含义如下&#xff1a; i …

ASP.NET MVC3 中的AJAX

示例演示一个链接&#xff0c;点击后利用Ajax更新特定id的标签中的内容 首先在_Layout.cshtml中加入js <script src"Url.Content("~/Scripts/jquery-1.5.1.min.js")" type"text/javascript"></script> <script src"Url.Cont…

如何判断注册用户是否已经存在(membership验证)

如何判断注册用户是否已经存在&#xff08;membership验证&#xff09; MembershipCreateStatus iStatus; Membership.CreateUser(username, password, email, question, answer, true, out iStatus); ErrirMsgText.Visible true; switch (iStatus) …

安装win_server_2012的方法

1、从微软官网下载评估版。 2、查看你的当前版本。以管理员身份运行cmd&#xff0c;然后输入“DISM /online /Get-CurrentEdition”。如果是评估版&#xff0c;例如Standard&#xff0c;把“ServerStandardEval”中的Eval这四个字母去掉&#xff0c;就是你的当前版本。下图表明…

Delphi控件的“拿来主义”

"一个优秀的Delphi程序员&#xff0c;不仅要会写控件&#xff0c;还要会使用控件。" 我还是一个半瓢水的程序员&#xff0c;因此目前为止我所能努力达到的境界是: 一个半瓢水的程序员&#xff0c;管他会不会写控件&#xff0c;只要能拿来改就可以了。 使用过Delphi的…

WRF参数配置(PartV)

&bc_control spec_bdy_width 此参数指定用于边界过渡的格点总行数&#xff0c;默认值为5。此参数只用于真实大气方案。参数的大小至少为spec_zone 和 relax_zone的和。 spec_zone 指定区域(specified zone)的格点数&#xff0c;默认值为 1。指定边条件时起作用。 relax…

DHCP中继处理办法

这两天一直在客户这边测试DHCP&#xff0c;由于客户的网络是现成的server 2008 是后来加上去的&#xff0c;所以没有多的IP地址用于测试&#xff0c;只好拿客户的楼层网段来测试&#xff0c;由于需要跨VLAN实行DHCP地址分配&#xff0c;所有需要做DHCP中继。废话不多说&#xf…

富士 FinePix F401

功能特点 F401尽管外型娇小&#xff0c;但功能并不弱&#xff0c;机身内搭载Super CCD III&#xff0c;实际CCD画素210万&#xff0c;纪录画素可达400万&#xff08;23041728像素&#xff09;&#xff0c;由于Super CCD III大幅改善噪声&#xff0c;有效ISO感光度提升到1600度&…

C++远航之封装篇——默认构造函数、初始化列表、拷贝构造函数

1、默认构造函数 没有参数&#xff1b;若有参数&#xff0c;则一定全部都有默认的参数值。 2、初始化列表 &#xff08;1&#xff09;概念 &#xff08;2&#xff09;特性 建议用初始化列表来初始化数据成员。初始化列表先于构造函数执行&#xff1b;初始化列表只能用于构造函…

性能测试流程-各阶段的工作

1 性能测试流程1.1 性能测试计划阶段测试计划阶段主要工作如下&#xff1a;1、明确测试对象2、定义测试目标3、定义测试通过的标准4、规划测试进度5、规划测试参与人员&#xff08;需求、开发、测试、运维和配置&#xff09;6、申请测试资源7、风险控制1.2 性…

恢复IE8自带的源代码查看器

各种知名不知名的文本编辑软件安完了之后&#xff0c;IE8的默认源代码编辑器也被改了很多次其实IE8默认已经不使用记事本了&#xff0c;带有语法高亮和行号功能的查看器很好用&#xff0c;堪比FF的查看器了。而且占用资源很少&#xff0c;跑起来很快&#xff0c;所以&#xff0…

C++远航之封装篇——析构函数

一、为什么需要析构函数&#xff1f; 见博客&#xff1a;http://blog.csdn.net/zhhymh/article/details/6239832 二、析构函数的理解 1、格式 2、必要性 释放资源。 3、什么时候调用&#xff1f; delet p时会调用析构函数&#xff1b;程序返回时&#xff0c;也会调用析构函数…

生成的数据库脚本没有注释?

1,选择Database->Generate Database 选中Generate name in empty comment就可以了转载于:https://www.cnblogs.com/hongjiumu/archive/2012/08/02/2620379.html

M.GetSum(); Myclass::GetSum(); // 调用类的静态成员函数。转载于:https://www.cnblogs.com/sunbines/p/11227051.html

HDU 1312

View Code 1 #include<stdio.h>2 #include<stdlib.h>3 #include<string.h>4 以前用BFS写过&#xff0c;这次改用DFS写发现DFS掌握的不好&#xff0c;挑了好久才过&#xff0c;今下午才AC了两道题&#xff0c;还要加紧练习DFS&#xff01;5 int row,col,coun…

C++远征之封装篇——对象数组,对象成员

一、对象数组 &#xff08;1&#xff09;如下&#xff0c;delet [ ]p时&#xff0c;会调用三次析构函数 &#xff08;2&#xff09;细节 实例化对象数组时&#xff0c;每一个对象的构造函数都会被执行。销毁对象数组时&#xff0c;每一个对象的析构函数都会被执行。 二、对象成…

linux sudo 免密码

修改/etc/sudoers文件为使用sudo命令的用户格式 username ALL(ALL) NOPASSWD: ALL转载于:https://www.cnblogs.com/xuedong09/p/3454794.html

shell 替换字符串的几种方法,变量替换${},sed,awk

变量a是一个带空格的字符串&#xff0c;现在用"hdpusr400"替换变量a中的"hduser302"&#xff1a; 1 [liusiyilocalhost ~]$ echo $a2 -rw-r----- 3 hduser302 hduser302 3336 2019-07-12 17:35 /apps/hduser302/student/properties/xxyy/IMP_00004_XXYY_US…

基于sqlite数据库的C语言编程

一 SQLITE 操作入门 sqlite 提供的是一些 C 函数接口&#xff0c;你可以用这些函数操作数据库。通过使用这些接口&#xff0c;传递一些标准 sql 语句&#xff08;以 char * 类型&#xff09;给 sqlite 函数&#xff0c; sqlite 就会为你操作数据库。 sqlite 跟 MS 的 access 一…