spring-framework / org.springframework.web.filter / DelegatingFilterProxy

DelegatingFilterProxy

open class DelegatingFilterProxy : GenericFilterBean

Proxy for a standard Servlet Filter, delegating to a Spring-managed bean that implements the Filter interface. Supports a "targetBeanName" filter init-param in web.xml, specifying the name of the target bean in the Spring application context.

web.xml will usually contain a DelegatingFilterProxy definition, with the specified filter-name corresponding to a bean name in Spring's root application context. All calls to the filter proxy will then be delegated to that bean in the Spring context, which is required to implement the standard Servlet Filter interface.

This approach is particularly useful for Filter implementation with complex setup needs, allowing to apply the full Spring bean definition machinery to Filter instances. Alternatively, consider standard Filter setup in combination with looking up service beans from the Spring root application context.

NOTE: The lifecycle methods defined by the Servlet Filter interface will by default not be delegated to the target bean, relying on the Spring application context to manage the lifecycle of that bean. Specifying the "targetFilterLifecycle" filter init-param as "true" will enforce invocation of the Filter.init and Filter.destroy lifecycle methods on the target bean, letting the servlet container manage the filter lifecycle.

As of Spring 3.1, DelegatingFilterProxy has been updated to optionally accept constructor parameters when using Servlet 3.0's instance-based filter registration methods, usually in conjunction with Spring 3.1's org.springframework.web.WebApplicationInitializer SPI. These constructors allow for providing the delegate Filter bean directly, or providing the application context and bean name to fetch, avoiding the need to look up the application context from the ServletContext.

This class was originally inspired by Spring Security's FilterToBeanProxy class, written by Ben Alex.

Author
Juergen Hoeller

Author
Sam Brannen

Author
Chris Beams

Since
1.2

See Also
#setTargetBeanName#setTargetFilterLifecyclejavax.servlet.Filter#doFilterjavax.servlet.Filter#initjavax.servlet.Filter#destroy#DelegatingFilterProxy(Filter)#DelegatingFilterProxy(String)#DelegatingFilterProxy(String, WebApplicationContext)javax.servlet.ServletContext#addFilter(String, Filter)org.springframework.web.WebApplicationInitializer

Constructors

<init>

DelegatingFilterProxy()

Create a new DelegatingFilterProxy. For traditional (pre-Servlet 3.0) use in web.xml.

DelegatingFilterProxy(delegate: Filter)

Create a new DelegatingFilterProxy with the given Filter delegate. Bypasses entirely the need for interacting with a Spring application context, specifying the target bean name, etc.

For use in Servlet 3.0+ environments where instance-based registration of filters is supported.

DelegatingFilterProxy(targetBeanName: String)

Create a new DelegatingFilterProxy that will retrieve the named target bean from the Spring WebApplicationContext found in the ServletContext (either the 'root' application context or the context named by #setContextAttribute).

For use in Servlet 3.0+ environments where instance-based registration of filters is supported.

The target bean must implement the standard Servlet Filter.

DelegatingFilterProxy(targetBeanName: String, wac: WebApplicationContext)

Create a new DelegatingFilterProxy that will retrieve the named target bean from the given Spring WebApplicationContext.

For use in Servlet 3.0+ environments where instance-based registration of filters is supported.

The target bean must implement the standard Servlet Filter interface.

The given WebApplicationContext may or may not be refreshed when passed in. If it has not, and if the context implements ConfigurableApplicationContext, a refresh() will be attempted before retrieving the named target bean.

This proxy's Environment will be inherited from the given WebApplicationContext.

Functions

destroy

open fun destroy(): Unit

doFilter

open fun doFilter(request: ServletRequest, response: ServletResponse, filterChain: FilterChain): Unit

getContextAttribute

open fun getContextAttribute(): String

Return the name of the ServletContext attribute which should be used to retrieve the WebApplicationContext from which to load the delegate Filter bean.

setContextAttribute

open fun setContextAttribute(contextAttribute: String): Unit

Set the name of the ServletContext attribute which should be used to retrieve the WebApplicationContext from which to load the delegate Filter bean.

setTargetBeanName

open fun setTargetBeanName(targetBeanName: String): Unit

Set the name of the target bean in the Spring application context. The target bean must implement the standard Servlet Filter interface.

By default, the filter-name as specified for the DelegatingFilterProxy in web.xml will be used.

setTargetFilterLifecycle

open fun setTargetFilterLifecycle(targetFilterLifecycle: Boolean): Unit

Set whether to invoke the Filter.init and Filter.destroy lifecycle methods on the target bean.

Default is "false"; target beans usually rely on the Spring application context for managing their lifecycle. Setting this flag to "true" means that the servlet container will control the lifecycle of the target Filter, with this proxy delegating the corresponding calls.