Tomcat中的Filter
Filter
节选部分源码、源码版本 Tomcat8.5
说明
filter 是 Servlet 规范
filter 是在 <请求进入容器后>,执行 Servlet.service方法之前执行
Filter相关接口
public interface Filter {
// 初始化
void init(FilterConfig filterConfig) throws ServletException;
// 过滤器方法
void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException;
// 销毁方法
void destroy();
}
// 过滤器链接口
public interface FilterChain {
void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException;
}
- 重点关注:初始化方法、过滤器方法
初始化
// org.apache.catalina.core.StandardContext
@Override
protected synchronized void startInternal() throws LifecycleException {
// 准备好环境
if (ok) {
if (!filterStart()) {
ok = false;
}
}
}
private HashMap<String, ApplicationFilterConfig> filterConfigs =
new HashMap<>();
public boolean filterStart() {
// Instantiate and record a FilterConfig for each defined filter
boolean ok = true;
synchronized (filterConfigs) {
filterConfigs.clear();
// 从 web.xml 解析的 filter 数据中添加到 Map集合中
for (Entry<String,FilterDef> entry : filterDefs.entrySet()) {
// <filter-name> 的值
String name = entry.getKey();
try {
// 此处调用 Filter.init 方法,之后保存
ApplicationFilterConfig filterConfig =
new ApplicationFilterConfig(this, entry.getValue());
filterConfigs.put(name, filterConfig);
} catch (Throwable t) {
ok = false;
}
}
}
return ok;
}
- 在解析完项目后,启动该项目时调用:startInternal 方法
- 调用 filterStart 方法,初始化 Filter,保存 Filter到HashMap中
接收到请求的流程
- 收到请求后根据 请求路径,找到相应的过滤器链,执行 doFilter 方法
- 调用目标方法
// org.apache.catalina.core.StandardWrapperValve
// 节选部分代码
@Override
public final void invoke(Request request, Response response) {
// 创建执行Filter链
ApplicationFilterChain filterChain =
ApplicationFilterFactory.createFilterChain(request, wrapper, servlet);
// 调用链
if ((servlet != null) && (filterChain != null)) {
filterChain.doFilter(request.getRequest(),
response.getResponse());
}
// 释放掉 filterChain
if (filterChain != null) {
filterChain.release();
}
}
Line7:ApplicationFilterFactory.createFilterChain
// org.apache.catalina.core.ApplicationFilterFactory
public static ApplicationFilterChain createFilterChain(
ServletRequest request, Wrapper wrapper, Servlet servlet) {
// 判断 servlet 是否存在
if (servlet == null) { return null; }
ApplicationFilterChain filterChain = null;
if (request instanceof Request) {
Request req = (Request) request;
if (Globals.IS_SECURITY_ENABLED) {
filterChain = new ApplicationFilterChain();
} else {
// 判断 request 是否已经保存
filterChain = (ApplicationFilterChain) req.getFilterChain();
if (filterChain == null) {
filterChain = new ApplicationFilterChain();
req.setFilterChain(filterChain);
}
}
} else {
// Request dispatcher in use
filterChain = new ApplicationFilterChain();
}
filterChain.setServlet(servlet);
filterChain.setServletSupportsAsync(wrapper.isAsyncSupported());
// 从 Context 中获取所有的 FilterMap 映射信息
StandardContext context = (StandardContext) wrapper.getParent();
FilterMap filterMaps[] = context.findFilterMaps();
// 没有配置 filter,返回 null
if ((filterMaps == null) || (filterMaps.length == 0))
return (filterChain);
// Acquire the information we will need to match filter mappings
DispatcherType dispatcher =
(DispatcherType) request.getAttribute(Globals.DISPATCHER_TYPE_ATTR);
String requestPath = null;
// 获取请求 路径:index.html
Object attribute = request.getAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR);
if (attribute != null){
requestPath = attribute.toString();
}
String servletName = wrapper.getName();
for (int i = 0; i < filterMaps.length; i++) {
if (!matchDispatcher(filterMaps[i] ,dispatcher)) {
continue;
}
// 匹配 url-pattern
if (!matchFiltersURL(filterMaps[i], requestPath))
continue;
// 根据 配置的 filter-name 找到 filterConfig
ApplicationFilterConfig filterConfig = (ApplicationFilterConfig)
context.findFilterConfig(filterMaps[i].getFilterName());
if (filterConfig == null) {
continue;
}
filterChain.addFilter(filterConfig);
}
// Add filters that match on servlet name second
for (int i = 0; i < filterMaps.length; i++) {
if (!matchDispatcher(filterMaps[i] ,dispatcher)) {
continue;
}
if (!matchFiltersServlet(filterMaps[i], servletName))
continue;
ApplicationFilterConfig filterConfig = (ApplicationFilterConfig)
context.findFilterConfig(filterMaps[i].getFilterName());
if (filterConfig == null) {
// FIXME - log configuration problem
continue;
}
filterChain.addFilter(filterConfig);
}
return filterChain;
}
LIne10:执行 ApplicationFilterChain.doFilter 方法
public final class ApplicationFilterChain implements FilterChain {
private int pos = 0;
private int n = 0;
@Override
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
internalDoFilter(request,response);
}
private void internalDoFilter(ServletRequest request, ServletResponse response) {
// Call the next filter if there is one
if (pos < n) {
ApplicationFilterConfig filterConfig = filters[pos++];
try {
Filter filter = filterConfig.getFilter();
if (request.isAsyncSupported() && "false".equalsIgnoreCase(
filterConfig.getFilterDef().getAsyncSupported())) {
request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR, Boolean.FALSE);
}
// 调用具体Filter 的 doFilter 方法。
filter.doFilter(request, response, this);
} catch (IOException | ServletException | RuntimeException e) {
throw e;
}
return;
}
// 调用servlet实例
try {
if (ApplicationDispatcher.WRAP_SAME_OBJECT) {
lastServicedRequest.set(request);
lastServicedResponse.set(response);
}
if (request.isAsyncSupported() && !servletSupportsAsync) {
request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR,
Boolean.FALSE);
}
servlet.service(request, response);
} catch (IOException | ServletException | RuntimeException e) {
throw e;
} finally {
if (ApplicationDispatcher.WRAP_SAME_OBJECT) {
lastServicedRequest.set(null);
lastServicedResponse.set(null);
}
}
}
}
- Line22,根据链中的具体的 Filter,调用 doFilter 方法
- LIne40,调用 servlet.service(request, response)【此处 与 springmvc 整合入口】
问题
- Request 为什么需要 保存 filterChain
req.setFilterChain(filterChain);
在 StandardWrapperValve 中会释放 filterChain
Tomcat中的Filter的更多相关文章
- 设计模式——责任链(结合Tomcat中Filter机制)
设计模式:责任链模式 说责任链之前,先引入一个场景,假如规定学生请假小于或等于 2 天,班主任可以批准:小于或等于 7 天,系主任可以批准:小于或等于 10 天,院长可以批准:其他情况不予批准:以此为 ...
- CAS 在 Tomcat 中实现单点登录
单点登录(Single Sign On , 简称 SSO )是目前比较流行的服务于企业业务整合的解决方案之一, SSO 使得在多个应用系统 中,用户只需要登录一次就可以访问所有相互信任的应用系统.CA ...
- 【IBM】使用 CAS 在 Tomcat 中实现单点登录
来源: IBM Developer http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/ 张 涛 (zzhangt@cn.ibm.com ...
- 使用 CAS 在 Tomcat 中实现单点登录
单点登录(Single Sign On , 简称 SSO )是目前比较流行的服务于企业业务整合的解决方案之一, SSO 使得在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.CAS ...
- Spring MVC中各个filter的用法
转载:http://blog.csdn.net/qyp1314/article/details/42023725 Spring MVC中各个filter的用法 2014-12-19 09:08 105 ...
- Spring Boot启动过程(六):内嵌Tomcat中StandardHost与StandardContext的启动
看代码有助于线上出现预料之外的事的时候,不至于心慌... StandardEngine[Tomcat].StandardHost[localhost]的启动与StandardEngine不在同一个线程 ...
- Tomcat中server.xml配置详解(2)
Tomcat中配置文件详解 Server.xml配置文件说明,以及Tomcat组件的说明 Tomcat服务器是由一系列可以配置的组件构成,其中核心组件是Catalina Servlet,它是最顶层组件 ...
- Spring Boot启动过程(六):内嵌Tomcat中StandardHost、StandardContext和StandardWrapper的启动
看代码有助于线上出现预料之外的事的时候,不至于心慌... StandardEngine[Tomcat].StandardHost[localhost]的启动与StandardEngine不在同一个线程 ...
- Tomcat中Pipeline
Pipeline 节选部分源码.源码版本 Tomcat8.5 处理模式 Pipeline--Valve是一种责任链模式,它和普通责任链模式有两点区别: 每个Pipeline都是有特定的Valve,而且 ...
随机推荐
- 基于easyUI实现经典系统主界面
此文章是基于 EasyUI+Knockout实现经典表单的查看.编辑 一. 相关文件介绍 1. home.jsp:系统主界面 <!DOCTYPE html PUBLIC "-//W3C ...
- 如何使DIV居中
小编我抛出一个问题: 有一个 div#wrapper 元素,高.宽度都未知.它其中有一个宽高都为 100px 的 div#box 元素,请你完成 CSS,使得 div#box 在 div#wrappe ...
- MUI框架-14-使用自定义icon图标、引入阿里巴巴矢量图标
MUI框架-14-使用自定义icon图标.引入阿里巴巴矢量图标 首先介绍介绍一下,前端必备的非常强大的 阿里巴巴矢量图标库:地址是:http://www.iconfont.cn/ 这里有丰富,精美,且 ...
- Android画廊效果
Android画廊效果 前言:Gallery是一个内部元素控件,可以水平滚动,并且可以把当前选择的子元素定位在它中心的布局组件:画廊Gallery一般用来显示可左右移动图片的列表(具体请看实例). 效 ...
- PRINCE2是什么意思?
PRINCE2是一种长期以来公认的项目管理方法,在英国公共部门广泛应用,在私营企业界也发展成为事实上的应用方法. PRINCE2开发于1989年,是一种结构性的项目管理方法,其所有者OGC(英国商务部 ...
- 排查在 Azure 中新建 Windows 虚拟机时遇到的经典部署问题
尝试创建新的 Azure 虚拟机 (VM) 时,遇到的常见错误是预配失败或分配失败. 当由于准备步骤不当,或者在从门户捕获映像期间选择了错误的设置而导致 OS 映像无法加载时,将发生预配失败. 当群集 ...
- 远程桌面到 Ubuntu 虚拟机
安装 Ubuntu 虚拟机,创建端口号为 3389 的 Endpoint. 安装 Gnome 桌面 复制 sudo apt-get update sudo apt-get install ubuntu ...
- Linux下postgres安装fuzzystrmatch其他拓展包
(1)安装gdal # wget http://download.osgeo.org/gdal/2.0.0/gdal-2.0.0.tar.gz # tar zxvf gdal-2.0.0.tar.gz ...
- SQL Server Management Studio 2012 键盘快捷键(转)
无论是对于DBA还是Developer,键盘快捷键都是很常用的,动动键盘可比鼠标快多了,不过SQL Server 2012对SSMS(SQL Server Management Studio)中的快捷 ...
- VS2017配置cuda9.1编译不过问题。
#if defined(_WIN32) #if _MSC_VER < 1600 || _MSC_VER > 1920 #error -- unsupported Microsoft Vis ...