Moment For Technology

Five, DelegatingFilterProxy source analysis

Posted on Dec. 3, 2022, 8:12 a.m. by Lisa Young
Category: The back-end Tag: The back-end shiro
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-nameshiroFilter/filter-name filter-classorg.springframework.web.filter.DelegatingFilterProxy/filter-class init-param param-nametargetFilterLifecycle/param-name param-valuetrue/param-value /init-param init-param param-nametargetBeanName/param-name param-valueshiroFilter22/param-value /init-param /filterCopy 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

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.