spring之DelegatingFilterProxy
DelegatingFilterProxy是一个标准servlet Filter的代理,代理实现了Filter接口的spring管理的Bean。支持一个在web.xml的init-param定义的"targetBeanName" filter,在spring applicationContext中指定了target bean的名称。
web.xml通常包含一个DelegatingFilterProxy的定义,它在root springContext中指定了filter-name及对应的bean。在springcontext中实现了标准servlet filter接口的bean在调用时都将被代理。
注意:Servlet Filter接口默认定义的lifecycle方法不会被代理到target bean,它依赖spring applicationContext管理target bean的lifecycle。在filter的属性targetFilterLifecycle的init-param中指定为true将强制执行Filter.init()和Filter.destory()方法,让Filter来管理filter的lifecycle。
从spring 3.1开始,DelegatingFilterProxy更新到可以支持servlet 3.0基于实例的filter注册方法来构建,通常和spring 3.1的 org.springframework.web.WebApplicationInitializer spi一起使用。这个类由spring security中Ben Alex 写的FilterToBeanProxy灵感触发而写的。
具体实现如下:
1.初始化方法
@Overrideprotected void initFilterBean() throws ServletException {synchronized (this.delegateMonitor) {if (this.delegate == null) {// If no target bean name specified, use filter name.if (this.targetBeanName == null) {this.targetBeanName = getFilterName();}// Fetch Spring root application context and initialize the delegate early,// if possible. If the root application context will be started after this// filter proxy, we'll have to resort to lazy initialization.WebApplicationContext wac = findWebApplicationContext();if (wac != null) {this.delegate = initDelegate(wac);}}}}
初始化代理
/*** Initialize the Filter delegate, defined as bean the given Spring* application context.* <p>The default implementation fetches the bean from the application context* and calls the standard {@code Filter.init} method on it, passing* in the FilterConfig of this Filter proxy.* @param wac the root application context* @return the initialized delegate Filter* @throws ServletException if thrown by the Filter* @see #getTargetBeanName()* @see #isTargetFilterLifecycle()* @see #getFilterConfig()* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)*/protected Filter initDelegate(WebApplicationContext wac) throws ServletException {Filter delegate = wac.getBean(getTargetBeanName(), Filter.class);if (isTargetFilterLifecycle()) {delegate.init(getFilterConfig());}return delegate;}
2. 触发代理方法
@Overridepublic 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 or DispatcherServlet registered?");}this.delegate = initDelegate(wac);}delegateToUse = this.delegate;}}// Let the delegate perform the actual doFilter operation. invokeDelegate(delegateToUse, request, response, filterChain);}
触发代理
/*** Actually invoke the delegate Filter with the given request and response.* @param delegate the delegate Filter* @param request the current HTTP request* @param response the current HTTP response* @param filterChain the current FilterChain* @throws ServletException if thrown by the Filter* @throws IOException if thrown by the Filter*/protected void invokeDelegate(Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)throws ServletException, IOException {delegate.doFilter(request, response, filterChain);}
3.销毁方法
/*** Destroy the Filter delegate.* Default implementation simply calls {@code Filter.destroy} on it.* @param delegate the Filter delegate (never {@code null})* @see #isTargetFilterLifecycle()* @see javax.servlet.Filter#destroy()*/protected void destroyDelegate(Filter delegate) {if (isTargetFilterLifecycle()) {delegate.destroy();}}
spring之DelegatingFilterProxy相关推荐
- 使用spring的DelegatingFilterProxy 写xss filter
最近需要写个xss过滤器,将访问网站的所有请求参数都进行xss过滤,过滤的api使用的是antisamy-1.4.4 java代码 public class XssFilter implements ...
- 在Spring MVC中使用Apache Shiro安全框架
我们在这里将对一个集成了Spring MVC+Hibernate+Apache Shiro的项目进行了一个简单说明.这个项目将展示如何在Spring MVC 中使用Apache Shiro来构建我们的 ...
- Spring Security 参考手册(一)
Spring Security 参考手册 Ben AlexLuke TaylorRob WinchGunnar Hillert Spring security 是一个强大的和高度可定制的身份验证和访问 ...
- Spring Security中文文档
Spring Security中文文档 来源:https://www.springcloud.cc/spring-security.html#overall-architecture 作者 Ben A ...
- Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】
源码请移步至: https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc ...
- Java 程序员如何使用 Shiro 框架
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:冷豪 来自:www.cnblogs.com/learnhow ...
- 30 分钟学会如何使用 Shiro
点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:程序员入职国企,1周上班5小时,晒出薪资感叹:腾讯当CEO也不去个人原创+1博客:点击前往,查看更多 作者:冷豪 ...
- 30分钟学会如何使用Shiro
转载自 https://www.cnblogs.com/learnhow/p/5694876.html 本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnian ...
- 10分钟学会如何使用Shiro
本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分 ...
最新文章
- Struts权威著作
- LAMP部署搭建————重要文件备份
- 《软件项目管理(第二版)》第 6 章——项目质量管理 重点部分总结
- SQLcode错误代码汇总和sqlstate=37000的解决方案
- FIN7 正在转向密码重置和软件供应链攻击
- Java经典编程题50道之三十二
- The requested resource is not available. 原因,成功解决
- c int转字符串_零基础如何学好Python 之int 数字整型类型 定义int()范围大小转换...
- python数据科学导论_R与Python手牵手:数据科学导论系列(包的载入)
- 小车--雷神、velodyne 雷达使用(IP)
- Shopee虾皮电商平台考试题附答案
- 思岚A3雷达官方ROS包将雷达的发布频率改为20HZ
- Vue项目使用SSR服务器渲染
- python局域网文件互传
- 非聚集索引中的临界点(Tipping Point)
- 单片空间后方交会 python实现
- 模拟器连接本地服务器
- 历届试题 大臣的旅费 java
- VC/MFC拖动窗口任意位置移动窗口
- 怎么翻译PDF文件内容?教你一招轻松翻译整篇PDF文件