类 DelegatingFilterProxy

java.lang.Object
cn.taketoday.web.servlet.filter.GenericFilterBean
cn.taketoday.web.servlet.filter.DelegatingFilterProxy
所有已实现的接口:
cn.taketoday.beans.factory.Aware, cn.taketoday.beans.factory.BeanNameAware, cn.taketoday.beans.factory.DisposableBean, cn.taketoday.beans.factory.InitializingBean, cn.taketoday.context.EnvironmentAware, cn.taketoday.core.env.EnvironmentCapable, ServletContextAware, Filter

public class DelegatingFilterProxy extends GenericFilterBean
Proxy for a standard Servlet Filter, delegating to a Frameworkmanaged bean that implements the Filter interface. Supports a "targetBeanName" filter init-param in web.xml, specifying the name of the target bean in the Framework application context.

web.xml will usually contain a DelegatingFilterProxy definition, with the specified filter-name corresponding to a bean name in Framework's root application context. All calls to the filter proxy will then be delegated to that bean in the Framework 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 Framework bean definition machinery to Filter instances. Alternatively, consider standard Filter setup in combination with looking up service beans from the Framework 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 Framework 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.

DelegatingFilterProxy has been updated to optionally accept constructor parameters when using a Servlet container's instance-based filter registration methods 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 Framework Security's FilterToBeanProxy class, written by Ben Alex.

从以下版本开始:
4.0 2022/2/20 23:17
作者:
Juergen Hoeller, Sam Brannen, Chris Beams, Harry Yang
另请参阅:
  • 字段详细资料

    • contextAttribute

      @Nullable private String contextAttribute
    • applicationContext

      @Nullable private cn.taketoday.context.ApplicationContext applicationContext
    • targetBeanName

      @Nullable private String targetBeanName
    • targetFilterLifecycle

      private boolean targetFilterLifecycle
    • delegate

      @Nullable private volatile Filter delegate
    • delegateMonitor

      private final Object delegateMonitor
  • 构造器详细资料

    • DelegatingFilterProxy

      public DelegatingFilterProxy()
      Create a new DelegatingFilterProxy. For traditional use in web.xml.
      另请参阅:
    • DelegatingFilterProxy

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

      For use with instance-based registration of filters.

      参数:
      delegate - the Filter instance that this proxy will delegate to and manage the lifecycle for (must not be null).
      另请参阅:
    • DelegatingFilterProxy

      public DelegatingFilterProxy(String targetBeanName)
      Create a new DelegatingFilterProxy that will retrieve the named target bean from the Framework WebApplicationContext found in the ServletContext (either the 'root' application context or the context named by setContextAttribute(java.lang.String)).

      For use with instance-based registration of filters.

      The target bean must implement the standard Servlet Filter interface.

      参数:
      targetBeanName - name of the target filter bean to look up in the Framework application context (must not be null).
      另请参阅:
    • DelegatingFilterProxy

      public DelegatingFilterProxy(String targetBeanName, @Nullable cn.taketoday.context.ApplicationContext wac)
      Create a new DelegatingFilterProxy that will retrieve the named target bean from the given Framework WebApplicationContext.

      For use with instance-based registration of filters.

      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.

      参数:
      targetBeanName - name of the target filter bean in the Framework 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 ServletContext as a fallback.
      另请参阅:
  • 方法详细资料

    • setContextAttribute

      public void setContextAttribute(@Nullable String contextAttribute)
      Set the name of the ServletContext attribute which should be used to retrieve the WebApplicationContext from which to load the delegate Filter bean.
    • getContextAttribute

      @Nullable public String getContextAttribute()
      Return the name of the ServletContext attribute which should be used to retrieve the WebApplicationContext from which to load the delegate Filter bean.
    • setTargetBeanName

      public void setTargetBeanName(@Nullable String targetBeanName)
      Set the name of the target bean in the Framework 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.

    • getTargetBeanName

      @Nullable protected String getTargetBeanName()
      Return the name of the target bean in the Framework application context.
    • setTargetFilterLifecycle

      public void setTargetFilterLifecycle(boolean targetFilterLifecycle)
      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 Framework 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.

    • isTargetFilterLifecycle

      protected boolean isTargetFilterLifecycle()
      Return whether to invoke the Filter.init and Filter.destroy lifecycle methods on the target bean.
    • initFilterBean

      protected void initFilterBean() throws ServletException
      从类复制的说明: GenericFilterBean
      Subclasses 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 Framework application context. Filter name and ServletContext will be available in both cases.

      This default implementation is empty.

      覆盖:
      initFilterBean 在类中 GenericFilterBean
      抛出:
      ServletException - if subclass initialization fails
      另请参阅:
    • doFilter

      public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException
      抛出:
      ServletException
      IOException
    • destroy

      public void destroy()
      从类复制的说明: GenericFilterBean
      Subclasses 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 Framework application context.

      This default implementation is empty.

      指定者:
      destroy 在接口中 cn.taketoday.beans.factory.DisposableBean
      指定者:
      destroy 在接口中 Filter
      覆盖:
      destroy 在类中 GenericFilterBean
    • findApplicationContext

      @Nullable protected cn.taketoday.context.ApplicationContext findApplicationContext()
      Return the WebApplicationContext passed in at construction time, if available. Otherwise, attempt to retrieve a WebApplicationContext from the ServletContext attribute with the configured name if set. Otherwise look up a WebApplicationContext under the well-known "root" application context attribute. The WebApplicationContext must have already been loaded and stored in the ServletContext before this filter gets initialized (or invoked).

      Subclasses may override this method to provide a different WebApplicationContext retrieval strategy.

      返回:
      the WebApplicationContext for this proxy, or null if not found
      另请参阅:
    • initDelegate

      protected Filter initDelegate(cn.taketoday.context.ApplicationContext wac) throws ServletException
      Initialize the Filter delegate, defined as bean the given Framework application context.

      The default implementation fetches the bean from the application context and calls the standard Filter.init method on it, passing in the FilterConfig of this Filter proxy.

      参数:
      wac - the root application context
      返回:
      the initialized delegate Filter
      抛出:
      ServletException - if thrown by the Filter
      另请参阅:
    • invokeDelegate

      protected void invokeDelegate(Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException
      Actually invoke the delegate Filter with the given request and response.
      参数:
      delegate - the delegate Filter
      request - the current HTTP request
      response - the current HTTP response
      filterChain - the current FilterChain
      抛出:
      ServletException - if thrown by the Filter
      IOException - if thrown by the Filter
    • destroyDelegate

      protected void destroyDelegate(Filter delegate)
      Destroy the Filter delegate. Default implementation simply calls Filter.destroy on it.
      参数:
      delegate - the Filter delegate (never null)
      另请参阅: