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. iOS tabbar点击动画效果实现

    正常情况下,我们点击tabbar都只有一个变色效果,但有时候,如果我们想给它添加一个点击动画,该如何做呢? 先上几个效果图: 1.先放大,再缩小 2.Z轴旋转               3.Y轴位移 ...

  2. 玩转 Ceph 的正确姿势

    玩转 Ceph 的正确姿势 本文先介绍 Ceph, 然后会聊到一些正确使用 Ceph 的姿势:在集群规模小的时候,Ceph 怎么玩都没问题:但集群大了(到PB级别),这些准则可是保证集群健康运行的不二 ...

  3. css3 UI 修饰——回顾

    1.box-shadow 属性向框添加一个或者多个阴影. 语法: box-shadow: h-shadow v-shadow blur spread color inset h-shadow 必须,水 ...

  4. 转自scutan 常用的Linux编程库

    库                       头文件                       描述libGL.so                                        ...

  5. imageNamed 与 initWithContentsOfFile 区别

    1.imageNamed: UIImage *image = [UIImage imageNamed:"]; UIImage的类方法 第一次读取图片的时候,先把这个图片放到缓存中,下次再使用 ...

  6. 《解决在Word中为汉子插入拼音及音标的问题》

    说明:本人使用的是Word2007版本.以下示例都是基于本人电脑操作.如有疑问,欢迎留言交流. [1]为word中的一些文字添加拼音及音标. [2]开始为文字添加拼音及音标. 选中要添加拼音及音标的文 ...

  7. JS验证电话号是否合法

    /******************** 函数名称:IsTelephone 函数功能:固话,手机号码检查函数,合法返回true,反之,返回false 函数参数:obj,待检查的号码 检查规则: (1 ...

  8. Codeforce 水题报告(2)

    又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...

  9. vmware安装黑苹果教程

    1.下载最新版vmware http://pan.baidu.com/s/1pLD8wxt 秘钥:UY192-0DW12-M81XY-DWW7C-MCKU6 2.确保hyper-v功能已关闭 3.下载 ...

  10. php代码审计--sql注入

    sql注入是web安全中最常见,也是平常中危害最大的漏洞. 最近在学习代码审计,拿自己审核的一段代码做个笔记. 1.sql语句拼接可能引起sql注入 很多偷懒的程序员对于没有过滤的参数,直接将其拼接到 ...