簡體   English   中英

FilterChainProxy的doFilter方法如何工作?

[英]How does the doFilter method of the FilterChainProxy work?

我正在查看org.springframework.security.web.FilterChainProxy類的源代碼。 我想了解其doFilter方法的工作方式。 以下是代碼。

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException 
{
    FilterInvocation fi = new FilterInvocation(request, response, chain);
    List<Filter> filters = getFilters(fi.getRequestUrl());

    if (filters == null || filters.size() == 0) {
        if (logger.isDebugEnabled()) {
            logger.debug(fi.getRequestUrl() +
                    filters == null ? " has no matching filters" : " has an empty filter list");
        }

        chain.doFilter(request, response);

        return;
    }

    VirtualFilterChain virtualFilterChain = new VirtualFilterChain(fi, filters);
    virtualFilterChain.doFilter(fi.getRequest(), fi.getResponse());

}

我的理解是,如果我在web.xml中定義了與Spring不相關的自定義過濾器,它們將包含在傳遞給FilterChainProxy的FilterChain對象中(我知道這是通過DelegatingFilterProxy發生的)。 那是對的嗎?

我認為當web.xml中定義了非spring過濾器並且應用程序上下文中沒有定義過濾器時,IF塊就會執行。

VirtualFilterChain在這里滿足應用程序文本中定義的過濾器。

If塊中有一個return語句,可防止執行VirtualFilterChain部分。

但是,這如何處理web.xml中定義的過濾器和應用程序上下文中定義的過濾器?

“ filterChain”參數是指在web.xml中定義的Servlet過濾器。 在DelegatingFilterProxy.java中查看此代碼

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

    // Lazily initialize the delegate if necessary.
    Filter delegateToUse = this.delegate;
    if (delegateToUse == null) {
        ...
    }

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

invokeDelegate(...)調用FilterChainProxy的doFilter(...)方法。

List<Filter> filters = getFilters(fi.getRequestUrl());

生成與給定url匹配的Spring Security過濾器列表( 本節中列出了一些過濾器)。

如果沒有Spring Security過濾器與requestUrl匹配,則執行將繼續進行到web.xml中定義的其余過濾器。 這就是if()塊的用途。

virtualFilterChain.doFilter(fi.getRequest(), fi.getResponse());

這就是Spring Security過濾器的doFilter(...)方法被調用的地方。 因此,例如,如果您將UsernamePasswordAuthenticationFilter作為配置的過濾器之一,則virtualFilterChain.doFilter(...)最終將調用UsernamePasswordAuthenticationFilter的doFilter(...)方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM