org.springframework.web.filter.DelegatingFilterProxy

一般情况,创建一个Filter是交给自己来实现的。基于servlet规范,在web.xml中配置,自定义filter实现Filter接口:

public interface Filter {
void init(FilterConfig var1) throws ServletException; void doFilter(ServletRequest var1, ServletResponse var2, FilterChain var3) throws IOException, ServletException; void destroy();
}

但是从shiro的操作方式来看,这个反过来了:spring提供了DelegatingFilterProxy作为通用Filter代理类,shiro以factoryBean的形式来构造自己的filter。

那么这个DelegatingFilterProxy究竟干了些什么?

  • 利用初始化方法获取被代理的对象delegate
protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
Filter delegate = wac.getBean(getTargetBeanName(), Filter.class);
if (isTargetFilterLifecycle()) {
delegate.init(getFilterConfig());
}
return delegate;
}
  • 通过doFilter方法来调用目标过滤器的doFilter方法
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws ServletException, IOException { // Lazily initialize the delegate if necessary.
Filter delegateToUse = this.delegate;
if (delegateToUse == null) {
synchronized (this.delegateMonitor) {
if (this.delegate == null) {
WebApplicationContext wac = findWebApplicationContext();
if (wac == null) {
throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
}
this.delegate = initDelegate(wac);
}
delegateToUse = this.delegate;
}
} // Let the delegate perform the actual doFilter operation.
invokeDelegate(delegateToUse, request, response, filterChain);
} protected void invokeDelegate(
Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
throws ServletException, IOException { delegate.doFilter(request, response, filterChain);
}
  • 通过destory方法来释放一些资源
@Override
public void destroy() {
Filter delegateToUse = this.delegate;
if (delegateToUse != null) {
destroyDelegate(delegateToUse);
}
} protected void destroyDelegate(Filter delegate) {
if (isTargetFilterLifecycle()) {
delegate.destroy();
}
}

这个三个操作就完全将详细的实现转移到被代理的Filter。因为spring是所有bean的容器,这个filter的实际创建就交给了其它框架,但是前提是,这些框架必须要实现FactoryBean接口。所以有了shiro的ShiroFilterFactoryBean(springSecurity原理应该跟这个差不多)。

最新文章

  1. jmeter(四)Sample之http请求
  2. Visual Studio 2013 各个版本的产品密钥
  3. noip2006解题报告
  4. cheat-linux命令行实用助记工具
  5. WCF中使用控件的委托,线程中的UI委托
  6. Oracle RAC 服务器端连接负载均衡(Load Balance)
  7. 实例源码--IOS高仿微信打飞机游戏(完整功能)
  8. java mail实现Email的发送,完整代码
  9. [FTP] FTPClient--FTP操作帮助类,上传下载,文件,目录操作 (转载)
  10. IOS支付宝支付出现6002问题的解决办法
  11. GitHub问题之恢复本地被删除的文件
  12. 一步一步挖出Compute
  13. 窗体透明,但窗体上的控件不透明(简单好用)good
  14. android设备中USB转串口demo 下载
  15. iOS7以后的侧滑返回上一页
  16. SecureCRT上传bash: rz: command not found
  17. c++(数据选择)
  18. [转帖]Zoom
  19. 网络克隆 主要是 PXE网卡启动 随笔
  20. 微信屏蔽APP分享链接的解决方案原理,剖析微信域名防封技术

热门文章

  1. 使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,报异常的解决方法
  2. PHP扩展--Yaf框架安装
  3. 51nod 1806 wangyurzee的树
  4. 2017-2018-1 20179205《Linux内核原理与设计》第十周作业
  5. Django rest framework + Vue简单示例
  6. 运维开发:python websocket网页实时显示远程服务器日志信息
  7. 【swupdate文档 一】嵌入式系统的软件管理
  8. markdown===在新窗口中打开网址的解决办法,以及其他遗留问题!
  9. linux内核网络接收数据流程图【转】
  10. OPENSOLARIS source