`

DelegatingFilterProxy-api

阅读更多
 

DelegatingFilterProxy-api

 

  译者注:

为什么用DelegatingFilterProxy?窃以为,这样子做可以将DelegatingFilterProxy所代理的filter可以作为spring的bean,受到

spring的管理。而且有一点,我觉得应该注意的是,因为web应用的启动顺序是:listener->filter->servlet的,而spring容器的

启动主要是通过以下两种方式实现的:启动ContextLoaderListener或者ContextLoaderServlet的,所以,要

DelegatingFilterProxy所代理的filter可以作为spring的bean的话,就必须使用

启动ContextLoaderListener的方式。

by kaiwii

/**
 * Proxy for a standard Servlet 2.3 Filter, delegating to a Spring-managed
 * bean that implements the Filter interface. Supports a "targetBeanName"
 * filter init-param in <code>web.xml</code>, specifying the name of the
 * target bean in the Spring application context.
 *翻译:

类DelegatingFilterProxy作为标准的Servlet 2.3 Filter的代理,但是Filter的实际工作是通过将任务

委派给spring管理的而且实现了Filter接口的bean来完成的。通过在web.xml文件的名为"targetBeanName"
的filter的init-param变量来指定到底Spring容器(Spring application context)中哪个bean来作为实际执行

filter任务的bean(target bean)。


 * <p><code>web.xml</code> will usually contain a DelegatingFilterProxy definition,
 * with the specified <code>filter-name</code> 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 2.3 Filter interface.

翻译:

web.xml通常会包括DelegatingFilterProxy的定义,其中的filter-name项会对应spring容器中的一个bean的名字。

所有的调用请求都会首先发送到这个filter代理中去,然后再委派到spring容器中的这个bean,当然,这个bean必须要

实现标准的Servlet 2.3 Filter 接口。
 *
 * <p>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.
 *翻译:

如果需要将Spring的bean命名机制应用到Filter实例上来完成复杂得启动任务,使用类DelegatingFilterProxy

就是一个很好的选择。除此之外,当标准的FIlter启动的时候需要使用到Spring容器里面的service bean的时候,

使用类DelegatingFilterProxy也是一个很好的选择。

 

 * <p><b>NOTE:</b> The lifecycle methods defined by the Servlet Filter interface
 * will by default <i>not</i> 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 <code>Filter.init</code> and <code>Filter.destroy</code> lifecycle methods
 * on the target bean, letting the servlet container manage the filter lifecycle.
 *翻译:

注意:Servlet Filter 接口中定义的生命周期函数默认不会被target bean自己执行(指:不是由服务器容器来调用),而是由spring容器(Spring application context )

执行来管理这个bean的生命周期。如果将名为"targetFilterLifecycle"的filter init-param参数设定为"true"的时候,target bean的Filter.init和Filter.destroy的

生命周期函数的调用就可以被执行了,换句话说,服务器容器(servlet container )就可以管理filter的生命周期了。


 * <p>This class is inspired by Acegi Security's FilterToBeanProxy class,
 * written by Ben Alex.

翻译:

这个类原型是FilterToBeanProxy 类
 *
 * @author Juergen Hoeller
 * @author Sam Brannen
* @author 译者:kaiwii

 * @since 1.2
 * @see #setTargetBeanName
 * @see #setTargetFilterLifecycle
 * @see javax.servlet.Filter#doFilter
 * @see javax.servlet.Filter#init
 * @see javax.servlet.Filter#destroy
 */
public class DelegatingFilterProxy extends GenericFilterBean {

 private String contextAttribute;

 private String targetBeanName;

 private boolean targetFilterLifecycle = false;

 private Filter delegate;

 private final Object delegateMonitor = new Object();


 /**
  * Set the name of the ServletContext attribute which should be used to retrieve the
  * {@link WebApplicationContext} from which to load the delegate{@link Filter} bean.
  */
 public void setContextAttribute(String contextAttribute) {
  this.contextAttribute = contextAttribute;
 }

 /**
  * Return the name of the ServletContext attribute which should be used to retrieve the
  * {@link WebApplicationContext} from which to load the delegate{@link Filter} bean.
  */
 public String getContextAttribute() {
  return this.contextAttribute;
 }

 /**
  * Set the name of the target bean in the Spring application context.
  * The target bean must implement the standard Servlet 2.3 Filter interface.
  * <p>By default, the <code>filter-name</code> as specified for the
  * DelegatingFilterProxy in <code>web.xml</code> will be used.
  */
 public void setTargetBeanName(String targetBeanName) {
  this.targetBeanName = targetBeanName;
 }

 /**
  * Return the name of the target bean in the Spring application context.
  */
 protected String getTargetBeanName() {
  return this.targetBeanName;
 }

 /**
  * Set whether to invoke the <code>Filter.init</code> and
  * <code>Filter.destroy</code> lifecycle methods on the target bean.
  * <p>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.
  */
 public void setTargetFilterLifecycle(boolean targetFilterLifecycle) {
  this.targetFilterLifecycle = targetFilterLifecycle;
 }

 /**
  * Return whether to invoke the <code>Filter.init</code> and
  * <code>Filter.destroy</code> lifecycle methods on the target bean.
  */
 protected boolean isTargetFilterLifecycle() {
  return this.targetFilterLifecycle;
 }


 protected void initFilterBean() throws ServletException {
  // If no target bean name specified, use filter name.
  if (this.targetBeanName == null) {
   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.
  synchronized (this.delegateMonitor) {
   WebApplicationContext wac = findWebApplicationContext();
   if (wac != null) {
    this.delegate = initDelegate(wac);
   }
  }
 }

 public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
   throws ServletException, IOException {

  // Lazily initialize the delegate if necessary.
  Filter 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;
  }

  // Let the delegate perform the actual doFilter operation.
  invokeDelegate(delegateToUse, request, response, filterChain);
 }

 public void destroy() {
  Filter delegateToUse = null;
  synchronized (this.delegateMonitor) {
   delegateToUse = this.delegate;
  }
  if (delegateToUse != null) {
   destroyDelegate(delegateToUse);
  }
 }


 /**
  * Retrieve a <code>WebApplicationContext</code> from the <code>ServletContext</code>
  * attribute with the {@link #setContextAttribute configured name}. The
  * <code>WebApplicationContext</code> must have already been loaded and stored in the
  * <code>ServletContext</code> before this filter gets initialized (or invoked).
  * <p>Subclasses may override this method to provide a different
  * <code>WebApplicationContext</code> retrieval strategy.
  * @return the WebApplicationContext for this proxy, or <code>null</code> if not found
  * @see #getContextAttribute()
  */
 protected WebApplicationContext findWebApplicationContext() {
  String attrName = getContextAttribute();
  if (attrName != null) {
   return WebApplicationContextUtils.getWebApplicationContext(getServletContext(), attrName);
  }
  else {
   return WebApplicationContextUtils.getWebApplicationContext(getServletContext());
  }
 }

 /**
  * Initialize the Filter delegate, defined as bean the given Spring
  * application context.
  * <p>Default implementation fetches the bean from the application context
  * and calls the standard <code>Filter.init</code> method on it, passing
  * in the FilterConfig of this Filter proxy.
  * @param wac the root application context
  * @return the initialized delegate Filter
  * @throws ServletException if thrown by the Filter
  * @see #getTargetBeanName()
  * @see #isTargetFilterLifecycle()
  * @see #getFilterConfig()
  * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
  */
 protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
  Filter delegate = (Filter) wac.getBean(getTargetBeanName(), Filter.class);
  if (isTargetFilterLifecycle()) {
   delegate.init(getFilterConfig());
  }
  return delegate;
 }

 /**
  * Actually invoke the delegate Filter with the given request and response.
  * @param delegate the delegate Filter
  * @param request the current HTTP request
  * @param response the current HTTP response
  * @param filterChain the current FilterChain
  * @throws ServletException if thrown by the Filter
  * @throws IOException if thrown by the Filter
  */
 protected void invokeDelegate(
   Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
   throws ServletException, IOException {

  delegate.doFilter(request, response, filterChain);
 }

 /**
  * Destroy the Filter delegate.
  * Default implementation simply calls <code>Filter.destroy</code> on it.
  * @param delegate the Filter delegate (never <code>null</code>)
  * @see #isTargetFilterLifecycle()
  * @see javax.servlet.Filter#destroy()
  */
 protected void destroyDelegate(Filter delegate) {
  if (isTargetFilterLifecycle()) {
   delegate.destroy();
  }
 }

}

分享到:
评论

相关推荐

    DelegatingFilterProxy示例

    DelegatingFilterProxy代码示例,包含普通filter和代理filter两个示例,帮助加深对DelegatingFilterProxy的理解。

    SPRING API 2.0.CHM

    DelegatingFilterProxy DelegatingIntroductionInterceptor DelegatingJob DelegatingMessageSource DelegatingNavigationHandlerProxy DelegatingPhaseListenerMulticaster DelegatingRequestProcessor ...

    Spring Security-3.0.1中文官方文档(翻译版)

    servlet-api-provision B.1.1.2. path-type B.1.1.3. lowercase-comparisons B.1.1.4. realm B.1.1.5. entry-point-ref B.1.1.6. access-decision-manager-ref B.1.1.7. access-denied-page B.1.1.8....

    Spring Security 中文教程.pdf

    servlet-api-provision B.1.1.2. path-type B.1.1.3. lowercase-comparisons B.1.1.4. realm B.1.1.5. entry-point-ref B.1.1.6. access-decision-manager-ref B.1.1.7. access-denied-page B.1.1.8....

    SpringSecurity 3.0.1.RELEASE.CHM

    servlet-api-provision B.1.1.2. path-type B.1.1.3. lowercase-comparisons B.1.1.4. realm B.1.1.5. entry-point-ref B.1.1.6. access-decision-manager-ref B.1.1.7. access-denied-page B.1.1.8. once-...

    SSH集成代理2.0版和struts.xml中DelegatingActionProxy代理搭配

    SSH集成代理2.0版和struts.xml中DelegatingActionProxy代理搭配

    单点登录sso-shiro-cas-maven

    &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;targetFilterLifecycle&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/init-param&gt; ...

    简单配置 shiro + spring +springMVC+hibernate简单框架

    org.springframework.web.filter.DelegatingFilterProxy &lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;shiroFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; maven...

    gradle-spring-4-mvc-boilerplate

    如该软件包所指定的那样,DelegatingFilterProxy被认为可与Spring Web MVC一起使用,并且仅与Spring Web MVC一起使用(aka控制器,带或不带注释)。 它似乎不能与普通的servlet-jsps一起使用,因为您似乎正在尝试...

    Nginx安装包

    作为这次的主角,相信大家对redis应该都一定印象,redis是一款开源的高性能key-value数据库,拥有丰富的键值储存类型,并提供多种语言的API。 与一般数据库不同,redis是使用内存作为主存,而使用硬盘来实现数据持久...

    spring security 参考手册中文版

    13.1 DelegatingFilterProxy 112 13.2 FilterChainProxy 113 13.2.1绕过滤网链 115 13.3过滤器排序 115 13.4请求匹配和HttpFirewall 116 13.5与其他基于过滤器的框架一起使用 118 13.6高级命名空间配置 118 14.核心...

    inject-dependencies-into-your-filters

    Dead Code Rising 代码示例展示了如何使用 Spring 的 DelegatingFilterProxy 将依赖项注入过滤器。 在阅读文章。

    spring-web-2.5.jar

    org.springframework.web.filter.DelegatingFilterProxy.class org.springframework.web.filter.GenericFilterBean.class org.springframework.web.filter.Log4jNestedDiagnosticContextFilter.class org.spring...

    尚硅谷Shiro视频教程

    尚硅谷_Shiro_DelegatingFilterProxy · 06. 尚硅谷_Shiro_权限 URL 配置细节 · 07. 尚硅谷_Shiro_认证思路分析 · 08.尚硅谷_Shiro_实现认证流程 · 09.尚硅谷_Shiro_实现认证 Realm · 10.尚硅谷_Shiro_密码...

    Spring Security3 张卫滨(译)

    添加Spring DelegatingFilterProxy到web.xml文件...................................................... 20 添加Spring Security XML配置文件的应用到web.xml ................................................ ...

Global site tag (gtag.js) - Google Analytics