Class DelegatingFilterProxy
- All Implemented Interfaces:
- Filter,- Aware,- BeanNameAware,- DisposableBean,- InitializingBean,- EnvironmentAware,- EnvironmentCapable,- ServletContextAware
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 implementations 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(jakarta.servlet.FilterConfig) and
 Filter.destroy() lifecycle methods on the target bean, letting the
 Servlet container manage the filter lifecycle.
 
DelegatingFilterProxy can optionally accept constructor parameters
 when using a Servlet container's instance-based filter registration methods,
 usually in conjunction with Spring's
 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.
- Since:
- 1.2
- Author:
- Juergen Hoeller, Sam Brannen, Chris Beams
- See Also:
- 
- setTargetBeanName(java.lang.String)
- setTargetFilterLifecycle(boolean)
- Filter.doFilter(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, jakarta.servlet.FilterChain)
- Filter.init(jakarta.servlet.FilterConfig)
- Filter.destroy()
- DelegatingFilterProxy(Filter)
- DelegatingFilterProxy(String)
- DelegatingFilterProxy(String, WebApplicationContext)
- ServletContext.addFilter(String, Filter)
- WebApplicationInitializer
 
- 
Field SummaryFields inherited from class org.springframework.web.filter.GenericFilterBeanlogger
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a newDelegatingFilterProxy.DelegatingFilterProxy(Filter delegate) Create a newDelegatingFilterProxywith the givenFilterdelegate.DelegatingFilterProxy(String targetBeanName) Create a newDelegatingFilterProxythat will retrieve the named target bean from the SpringWebApplicationContextfound in theServletContext(either the 'root' application context or the context named bysetContextAttribute(java.lang.String)).DelegatingFilterProxy(String targetBeanName, WebApplicationContext wac) Create a newDelegatingFilterProxythat will retrieve the named target bean from the given SpringWebApplicationContext.
- 
Method SummaryModifier and TypeMethodDescriptionvoiddestroy()Subclasses may override this to perform custom filter shutdown.protected voiddestroyDelegate(Filter delegate) Destroy the Filter delegate.voiddoFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) protected WebApplicationContextReturn theWebApplicationContextpassed in at construction time, if available.Return the name of the ServletContext attribute which should be used to retrieve theWebApplicationContextfrom which to load the delegateFilterbean.protected StringReturn the name of the target bean in the Spring application context.protected FilterInitialize the Filter delegate, defined as bean the given Spring application context.protected voidSubclasses may override this to perform custom initialization.protected voidinvokeDelegate(Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain) Actually invoke the delegate Filter with the given request and response.protected booleanReturn whether to invoke theFilter.initandFilter.destroylifecycle methods on the target bean.voidsetContextAttribute(String contextAttribute) Set the name of the ServletContext attribute which should be used to retrieve theWebApplicationContextfrom which to load the delegateFilterbean.voidsetTargetBeanName(String targetBeanName) Set the name of the target bean in the Spring application context.voidsetTargetFilterLifecycle(boolean targetFilterLifecycle) Set whether to invoke theFilter.initandFilter.destroylifecycle methods on the target bean.Methods inherited from class org.springframework.web.filter.GenericFilterBeanaddRequiredProperty, afterPropertiesSet, createEnvironment, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, setBeanName, setEnvironment, setServletContext
- 
Constructor Details- 
DelegatingFilterProxypublic DelegatingFilterProxy()Create a newDelegatingFilterProxy. For traditional use inweb.xml.- See Also:
 
- 
DelegatingFilterProxyCreate a newDelegatingFilterProxywith the givenFilterdelegate. Bypasses entirely the need for interacting with a Spring application context, specifying the target bean name, etc.For use with instance-based registration of filters. - Parameters:
- delegate- the- Filterinstance that this proxy will delegate to and manage the lifecycle for (must not be- null).
- See Also:
 
- 
DelegatingFilterProxyCreate a newDelegatingFilterProxythat will retrieve the named target bean from the SpringWebApplicationContextfound in theServletContext(either the 'root' application context or the context named bysetContextAttribute(java.lang.String)).For use with instance-based registration of filters. The target bean must implement the standard Servlet Filter interface. - Parameters:
- targetBeanName- name of the target filter bean to look up in the Spring application context (must not be- null).
- See Also:
 
- 
DelegatingFilterProxyCreate a newDelegatingFilterProxythat will retrieve the named target bean from the given SpringWebApplicationContext.For use with instance-based registration of filters. The target bean must implement the standard Servlet Filter interface. The given WebApplicationContextmay or may not be refreshed when passed in. If it has not, and if the context implementsConfigurableApplicationContext, arefresh()will be attempted before retrieving the named target bean.This proxy's Environmentwill be inherited from the givenWebApplicationContext.- Parameters:
- targetBeanName- name of the target filter bean in the Spring application context (must not be- null).
- wac- the application context from which the target filter will be retrieved; if- null, an application context will be looked up from- ServletContextas a fallback.
- See Also:
 
 
- 
- 
Method Details- 
setContextAttributeSet the name of the ServletContext attribute which should be used to retrieve theWebApplicationContextfrom which to load the delegateFilterbean.
- 
getContextAttributeReturn the name of the ServletContext attribute which should be used to retrieve theWebApplicationContextfrom which to load the delegateFilterbean.
- 
setTargetBeanNameSet 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-nameas specified for the DelegatingFilterProxy inweb.xmlwill be used.
- 
getTargetBeanNameReturn the name of the target bean in the Spring application context.
- 
setTargetFilterLifecyclepublic void setTargetFilterLifecycle(boolean targetFilterLifecycle) Set whether to invoke theFilter.initandFilter.destroylifecycle 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. 
- 
isTargetFilterLifecycleprotected boolean isTargetFilterLifecycle()Return whether to invoke theFilter.initandFilter.destroylifecycle methods on the target bean.
- 
initFilterBeanDescription copied from class:GenericFilterBeanSubclasses may override this to perform custom initialization. All bean properties of this filter will have been set before this method is invoked.Note: This method will be called from standard filter initialization as well as filter bean initialization in a Spring application context. Filter name and ServletContext will be available in both cases. This default implementation is empty. - Overrides:
- initFilterBeanin class- GenericFilterBean
- Throws:
- ServletException- if subclass initialization fails
- See Also:
 
- 
doFilterpublic void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException - Throws:
- ServletException
- IOException
 
- 
destroypublic void destroy()Description copied from class:GenericFilterBeanSubclasses may override this to perform custom filter shutdown.Note: This method will be called from standard filter destruction as well as filter bean destruction in a Spring application context. This default implementation is empty. - Specified by:
- destroyin interface- DisposableBean
- Specified by:
- destroyin interface- Filter
- Overrides:
- destroyin class- GenericFilterBean
 
- 
findWebApplicationContextReturn theWebApplicationContextpassed in at construction time, if available. Otherwise, attempt to retrieve aWebApplicationContextfrom theServletContextattribute with the configured name if set. Otherwise look up aWebApplicationContextunder the well-known "root" application context attribute. TheWebApplicationContextmust have already been loaded and stored in theServletContextbefore this filter gets initialized (or invoked).Subclasses may override this method to provide a different WebApplicationContextretrieval strategy.- Returns:
- the WebApplicationContextfor this proxy, ornullif not found
- See Also:
 
- 
initDelegateInitialize the Filter delegate, defined as bean the given Spring application context.The default implementation fetches the bean from the application context and calls the standard Filter.initmethod on it, passing in the FilterConfig of this Filter proxy.- Parameters:
- wac- the root application context
- Returns:
- the initialized delegate Filter
- Throws:
- ServletException- if thrown by the Filter
- See Also:
 
- 
invokeDelegateprotected void invokeDelegate(Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException Actually invoke the delegate Filter with the given request and response.- Parameters:
- delegate- the delegate Filter
- request- the current HTTP request
- response- the current HTTP response
- filterChain- the current FilterChain
- Throws:
- ServletException- if thrown by the Filter
- IOException- if thrown by the Filter
 
- 
destroyDelegateDestroy the Filter delegate. Default implementation simply callsFilter.destroyon it.- Parameters:
- delegate- the Filter delegate (never- null)
- See Also:
 
 
-