我在我的Spring MVC应用程序的web.xml
中看到了这个:
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
我试图弄清楚它为什么存在以及它是否真的有必要。
我在 Spring 文档中找到了这个解释,但它并没有帮助我理解:
它似乎暗示这个组件是将在
web.xml
中定义的 Servlet 和在 Spring 的 applicationContext.xml
中定义的组件之间的 "粘合剂"。
7.1 DelegatingFilterProxy
When using servlet filters, you obviously need to declare them in your
web.xml
, or they will be ignored by the servlet container. In Spring Security, the filter classes are also Spring beans defined in the application context and thus able to take advantage of Spring's rich dependency-injection facilities and lifecycle interfaces. Spring'sDelegatingFilterProxy
provides the link betweenweb.xml
and the application context.When using DelegatingFilterProxy, you will see something like this in the
web.xml
file:
<filter> <filter-name>myFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>myFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Notice that the filter is actually a
DelegatingFilterProxy
, and not the class that will actually implement the logic of the filter. WhatDelegatingFilterProxy
does is delegate the Filter's methods through to a bean which is obtained from the Spring application context. This enables the bean to benefit from the Spring web application context lifecycle support and configuration flexibility. The bean must implementjavax.servlet.Filter
and it must have the same name as that in the filter-name element. Read the Javadoc for DelegatingFilterProxy for more information
因此,如果我从我的web.xml
中删除这个,会发生什么?我的servlet将无法与Spring容器通信吗?**