Spring 里面定义了许多 Filter. 比如 OncePerRequestFilter。

如果我们自定义OncePerRequestFilter, 则可以配置到web.xml中进行一些拦截或日志操作。

问题是如何将spring filter bean 注入到 web.xml?

发现Spring Security 中有这种例子。我就阅读了下源码。发现如下。

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
</param-value>
</context-param> <!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter> <filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

其中,发现了DelegatingFilterProxy 这个类。然后读了下这个class的source。

节选如下:

    @Override
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) {
//这里在查找 applicationContext
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);
}

于是就清晰了。

基本上的配置就是,在web.xml 中配置 listener 初始化 spring application context.

然后配置 DelegatingFilterProxy,包装代理 FilterBean.

Spring DelegatingFilterProxy的更多相关文章

  1. Spring DelegatingFilterProxy解析

    以前DelegatingFilterProxy是在需要使用spring security 的时候在xml中配置,如下: <filter> <filter-name>spring ...

  2. spring DelegatingFilterProxy管理过滤器

    安全过滤器链 Spring Security的web架构是完全基于标准的servlet过滤器的.它没有在内部使用servlet或任何其他基于servlet的框架(比如spring mvc),所以它没有 ...

  3. spring DelegatingFilterProxy的原理及运用

    DelegatingFilterProxy的原理及使用 DelegatingFilterProxy就是一个对于servlet filter的代理,用这个类的好处主要是通过Spring容器来管理serv ...

  4. spring security原理图及其解释

    用户发出订单修改页面的请求,Access Decision Manager进行拦截,然后对比用户的授权和次页面需要的授权是不是有重合的部分,如果有重合的部分,那面页面就授权成功,如果失败就通知用户. ...

  5. 配置DelegatingFilterProxy使用Spring管理filter chain

    项目环境:JDK7 + Maven3.04 0. 项目使用springmvc作为controller层 1. 引入spring-security <dependency> <grou ...

  6. Spring MVC过滤器-委派过滤器代理(DelegatingFilterProxy)

    org.springframework.web.filter中有一个特殊的类——DelegatingFilterProxy,该类其实并不能说是一个过滤器,它的原型是FilterToBeanProxy, ...

  7. SPRING IN ACTION 第4版笔记-第九章Securing web applications-001-SpringSecurity简介(DelegatingFilterProxy、AbstractSecurityWebApplicationInitializer、WebSecurityConfigurerAdapter、@EnableWebSecurity、@EnableWebMvcS)

    一.SpringSecurity的模块 At the least, you’ll want to include the Core and Configuration modules in your ...

  8. shiro集成spring&工作流程&DelegatingFilterProxy

    1.集成Spring 参考文献: 新建web工程: ehcache-core来自Hibernate wen.xml <?xml version="1.0" encoding= ...

  9. spring之DelegatingFilterProxy

    DelegatingFilterProxy是一个标准servlet Filter的代理,代理实现了Filter接口的spring管理的Bean.支持一个在web.xml的init-param定义的&q ...

随机推荐

  1. JWPlayer使用指南

    http://support.jwplayer.com/customer/portal/articles/1499103-quickstart-reference <div id="m ...

  2. html标签大全(2)

    http标签详解 声明 1:这里的文字都是我从我自己csdn账号拷贝过来,是本人学习总结的结晶,所以请尊重本作品.2:如要要转载本文章,则要说明文字的出处.3:如有哪里不对欢迎指出. 在上一篇文章中主 ...

  3. Redis系列四(keepalived+lvs搭建负载均衡)

    1.安装Keepalived(主备服务器都要安装) 10.8.80.218  主服务器 10.8.80.217  备服务器 10.8.80.200  虚拟IP $ wget http://www.ke ...

  4. 【原创】梵高油画用深度卷积神经网络迭代十万次是什么效果? A neural style of convolutional neural networks

    作为一个脱离了低级趣味的码农,春节假期闲来无事,决定做一些有意思的事情打发时间,碰巧看到这篇论文: A neural style of convolutional neural networks,译作 ...

  5. mybatis入门-框架原理

    mybatis是什么 在说mybatis原理之前,我们有必要知道,mybatis到底是个什么东西.mybatis是一个持久层的框架.是一个不完全的ORM框架.因为它需要由程序员自己去写sql语句.但是 ...

  6. Arduino入门学习

    一直听到许多做物联网.智能家居的控制器使用的是Arduino,从师兄那里拿到了一块Arduino开发板,进行了一下午的学习,感觉这个适合小孩子们玩:) 废话少说,总结一下,便于以后可能会用得到.我主要 ...

  7. PhotoshopCC 如何使用动作文件ATN

    非常感谢公司的前端同事,今早给我推荐了一个很好用的插件 atn ,下面简单的总结下 导入 atn 插件的方法: 打开 photoshop 或者 photoshopCC 软件→点击 窗口菜单→找到 动作 ...

  8. WDCP下安装PHPWind

    创建整站跟新建站点的区别是创建整站会一并生成ftp跟mysql数据库 这边只要填写一个域名(如果你有域名就填写下域名 如果你没有域名 或者跟我一样到这步去申请域名的可以填写ECS公网ip否则无法访问新 ...

  9. Java实现GB2312文件转UTF8文件

    有些书带的光盘的源代码是GB2312编码.通常IDE的编码是UTF8.这样直接导入IDE会乱码. 这时候就需要把GB2312的文件转成UTF8的文件.转化的思路很简单,读入流初始化的时候告诉jvm是G ...

  10. Javascript基本语句

    1.单行语句是大家用的最多的,下面讲讲复合语句的用法. 用一对花括号括起来,处理的时候,可以用单句来对待.这样做的好处是避免复合语句中语句互相干扰执行. 语法如下: { var x=1111: var ...