[英]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.