org.springframework.web.filter.DelegatingFilterProxy的作用
一、类结构
DelegatingFilterProxy类存在与spring-web包中,其作用就是一个filter的代理,用这个类的好处是可以通过spring容器来管理filter的生命周期,还有就是,可以通过spring注入的形式,来代理一个filter执行,如shiro,下面会说到;有上图我们可以看到,DelegatingFilterProxy类继承GenericFilterBean,间接实现了Filter这个接口,故而该类属于一个过滤器。那么就会有实现Filter中init、doFilter、destroy三个方法。
二、代理具体实现
首先我们看init方法,我们知道当filter初始化时会执行init方法,从源码中我们可以找到具体代码,该方法在GenericFilterBean类中实现,具体功能是,将该类封装成spring特有形式的类,方便spring维护,并且调用initFilterBean方法,该方法放在子类(DelegatingFilterProxy)实现,该方法主要目的是,找到在spring中维护的目标filter,具体实现看下面代码:
- /**
- * Standard way of initializing this filter.
- * Map config parameters onto bean properties of this filter, and
- * invoke subclass initialization.
- * @param filterConfig the configuration for this filter
- * @throws ServletException if bean properties are invalid (or required
- * properties are missing), or if subclass initialization fails.
- * @see #initFilterBean
- */
- @Override
- public final void init(FilterConfig filterConfig) throws ServletException {
- Assert.notNull(filterConfig, "FilterConfig must not be null");
- if (logger.isDebugEnabled()) {
- logger.debug("Initializing filter '" + filterConfig.getFilterName() + "'");
- }
- this.filterConfig = filterConfig;
- // Set bean properties from init parameters.
- try {
//将该类封装成spring特有的bean形式,方便spring维护- PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
- BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
- ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
- bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, this.environment));
- initBeanWrapper(bw);
- bw.setPropertyValues(pvs, true);
- }
- catch (BeansException ex) {
- String msg = "Failed to set bean properties on filter '" +
- filterConfig.getFilterName() + "': " + ex.getMessage();
- logger.error(msg, ex);
- throw new NestedServletException(msg, ex);
- }
- // 该方法在子类中实现,我们可以到DelegatingFilterPoxy中去看看,具体完成了那些工作?
//1、找到要代理bean的id--》targetBeanName
//2、在spring,bean容器中找到具体被代理的filter--》delegate- initFilterBean();
- if (logger.isDebugEnabled()) {
- logger.debug("Filter '" + filterConfig.getFilterName() + "' configured successfully");
- }
- }
initFilterBean()该方法主要完成两个功能:
1、找到被代理类在spring中配置的id并赋值给targetBeanName。
2、使用找到的id从spring容器中找到具体被代理的类,并赋值给delegate
- @Override
- protected void initFilterBean() throws ServletException {
- synchronized (this.delegateMonitor) {
- if (this.delegate == null) {
- // If no target bean name specified, use filter name.
- if (this.targetBeanName == null) {
//找到要被代理的filter在spring中配置的id- 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) {
//找到具体被代理的filter- this.delegate = initDelegate(wac);
- }
- }
- }
- }
getFilterName()该方法的作用是,获取被代理的filter在spring中配置的id
- protected final String getFilterName() {
//找到被代理filter在spring中配置的id- return (this.filterConfig != null ? this.filterConfig.getFilterName() : this.beanName);
- }
initDelegate()该方法的作用是,从spring容器中获取到具体被代理的filter
- //找到被代理的filter
protected Filter initDelegate(WebApplicationContext wac) throws ServletException {- Filter delegate = wac.getBean(getTargetBeanName(), Filter.class);
- if (isTargetFilterLifecycle()) {
- delegate.init(getFilterConfig());
- }
- return delegate;
- }
- 到这里我们可以看出来,我们要代理的filter其实就是我们配置filter中的filter-name标签中的filterName了
- <filter-name>filterName</filter-name>
- 我们在来看看doFilter方法具体实现,该方法主要是使用被代理的filter,并调用invokeDelegate方法,
执行被代理filter的doFilter方法,具体实现,请看下面源码:
- @Override
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
- throws ServletException, IOException {
- // 得到被代理的filter
- 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;
- }
- }
- // 执行被代理filter的doFilter方法
- invokeDelegate(delegateToUse, request, response, filterChain);
- }
- invokeDelegate方法的作用就是执行被代理filter的doFilter方法
- protected void invokeDelegate(
- Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
- throws ServletException, IOException {
- delegate.doFilter(request, response, filterChain);
- }
- 看到这里我相信大家都明白DelegatingFilterPoxy是怎么回事了吧。下面我们看看spring+shiro是如何运用这个类的
- 三、运用
首先我们看web.xml具体配置,注意<filter-name>中配置的name,以name为id在spring的bean配置中找得到对应的bean
- <!-- Shiro Security filter-->
- <filter>
- <filter-name>shiroFilter</filter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
- <init-param>
- <param-name>targetFilterLifecycle</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>shiroFilter</filter-name>
- <url-pattern>/*</url-pattern>
- <dispatcher>REQUEST</dispatcher>
- <dispatcher>ERROR</dispatcher>
- </filter-mapping>
- spring对于代理filter配置
- <bean id="shiroFilter" class="com.auth.SpringShiroFilter"/>
- 第一次写博客,存在很多不足,希望大家见谅。
org.springframework.web.filter.DelegatingFilterProxy的作用的更多相关文章
- org.springframework.web.filter.DelegatingFilterProxy的理解
org.springframework.web.filter.DelegatingFilterProxy可以将filter交给spring管理. 我们web.xml中配置filter时一般采用下面这种 ...
- Spring管理过滤器:org.springframework.web.filter.DelegatingFilterProxy
配置web.xml <filter> <filter-name>springSecurityFilterChain</filter-name> ...
- org.springframework.web.filter.CharacterEncodingFilter
感谢:http://blog.csdn.net/heidan2006/article/details/3075730 很简单很实用的一个过滤器,当前台JSP页面和JAVA代码中使用了不同的字符集进行编 ...
- java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterEncodingFilter
今天在用git merge 新代码后报了如下错误:java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterE ...
- 通过org.springframework.web.filter.CharacterEncodingFilter定义Spring web请求的编码
通过类org.springframework.web.filter.CharacterEncodingFilter,定义request和response的编码.具体做法是,在web.xml中定义一个F ...
- java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast to javax.servlet.Filter
java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast ...
- SpringMVC(四):@RequestMapping结合org.springframework.web.filter.HiddenHttpMethodFilter实现REST请求
1)REST具体表现: --- /account/1 HTTP GET 获取id=1的account --- /account/1 HTTP DELETE 删除id=1的account ...
- Caused by: java.lang.ClassNotFoundException: org.springframework.web.filter.FormContentFilter
又是一个报错,我写代码真的是可以,所有的bug都会被我遇到,所有的问题我都能踩一遍,以前上学的时候同学就喜欢问我问题,因为他们遇到的问题,我早就遇到了......... 看看报错内容: 2019-04 ...
- SpringMVC------报错:java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterEncodingFilter
详细信息: java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterEncodingFilter 严重: E ...
随机推荐
- keydown - > keypress - > keyup
英文输入法: 事件触发顺序:keydown - > keypress - > keyup 中文输入法: firfox:输入触发keydown,回车确认输入触发keyup chr ...
- 【转】NAS 黑群晖 配置完成(不含硬盘),NAS能做什么?
在配黑群晖前,240元入手过一个艾美佳的NAS感受了下,功能倒还合适,就是配置太老,厂家固件也停止更新了,一直不太满意. 后来经常关注NAS1,发现现在X86的NAS也很好自己DIY了,就长草了,向女 ...
- XML读取信息并显示
这个类命名叫Message.cs namespace Common { public class Message { /// <summary> /// 信息编号 /// </sum ...
- 移动端分享到微信和QQ
关于在H5页面实现分享到微信和QQ,当初做的时候由于没有做过这方面的功能,也查了很多资料,找了很多插件,试了很多方法,大部分的都是点击后出现一个二维码,这不 符合我的需求,所以在网上找了一个 nati ...
- 使用HDFS客户端java api读取hadoop集群上的信息
本文介绍使用hdfs java api的配置方法. 1.先解决依赖,pom <dependency> <groupId>org.apache.hadoop</groupI ...
- 史上最难的一道Java面试题 (分析篇)
博客园 匠心零度 转载请注明原创出处,谢谢! 无意中了解到如下题目,觉得蛮好. 题目如下: public class TestSync2 implements Runnable { int b = 1 ...
- 模块:time,random,os,sys
时间模块 import time # print(time.time()) #时间戳 # print(time.strftime('%Y-%m-%d %X')) #格式化字符 # print(time ...
- php环境搭建工具推荐
楼楼最近由于一系列原因,使用了几款php环境搭建工具,安装配置方便,所以在这里推荐一下.第一款是XAMPP(网址http://www.xampps.com/),软件包原来的名字是 LAMPP,但是为了 ...
- 旅行(LCA)
Description N-1座桥连接着N个岛屿,每座桥都连接着某两个不同的岛屿,从任意一个岛屿都可以到达所有的其他岛屿,过桥需要缴纳人民币1元的过桥费. 由于某些不可透露的原因,Jason和他的2个 ...
- Fedora 下 Google-Chrome 经常出现僵尸进程的权宜办法
对于Chrome_ProcessL 和Chrome_FileThre这两僵尸进程,估计遇到过的人都对其各种无奈吧,放任不管吧,越来越多,然后卡死,只能另开个X环境或者在其他的TTY里干掉他俩再切回去, ...