DelegatingFilterProxy source analysisCopy the code

DelegatingFilterProxy

org.springframework.web.filter.DelegatingFilterProxy

Can see DelegatingFilterProxy GenericFilterBean class hierarchy, indirect implements the Filter this interface, so the class belongs to a Filter. The init, doFilter, and destroy methods of Filter are implemented.

Filter Introduction: www.cnblogs.com/xdp-gacl/p/…

GenericFilterBean; GenericFilterBean;Finally called

this.initFilterBean();

    protected void initFilterBean() throws ServletException {
    }

Copy the code

Now it’s ok to focus on initFilterBean()

public class DelegatingFilterProxy extends GenericFilterBean { private String contextAttribute; private WebApplicationContext webApplicationContext; private String targetBeanName; private boolean targetFilterLifecycle; private volatile Filter delegate; private final Object delegateMonitor; . protected void initFilterBean() throws ServletException { Object var1 = this.delegateMonitor; synchronized(this.delegateMonitor) { if(this.delegate == null) { if(this.targetBeanName == null) { this.targetBeanName =  this.getFilterName(); } WebApplicationContext wac = this.findWebApplicationContext(); if(wac ! = null) { this.delegate = this.initDelegate(wac); }}}}... }Copy the code

Inheritance in DelegatingFilterProxy GenericFilterBean rewrite initFilterBean (), can explain it now If you don’t want to use the filter – name: ShiroFilter can also use the value of targerBeanName

  1. If you do not set targetBeanName, the spring container will load an instance of filterbean () with the same name as filter-name.

Find the id configured in Spring for the proxy class and assign it to the targetBeanName.

2, use the found id to find the delegate class from the Spring container and assign it to the delegate

<! --Shiro filter --> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>targetBeanName</param-name> <param-value>shiroFilter22</param-value> </init-param> </filter>Copy the code

DelegatingFilterProxy

private String targetBeanName; private boolean targetFilterLifecycle; private volatile Filter delegate;

The getFilterName() method gets the id configured in Spring for the proxied filter

private volatile Filter delegate; Get filter

The initDelegate() method is used to get the specific proxy filter from the Spring container

Perform doFilter ()

The invokeDelegate method executes the doFilter method of the proxied filter

private boolean targetFilterLifecycle; The default is false to determine whether the targetFilterLifecycle property is false or true and whether to call the init(), destry() methods of the custom class